Iterative solution for factorial of a number

Share Your Love

Factorial can be evaluated recursively and its very difficult for large numbers. So here we writing code in recursive way using for loop and while loop.

Using For Loop:

C++ program for factorial of a number

// C++ program for factorial of a number
#include <iostream>
using namespace std;

// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
	int res = 1, i;
	for (i = 2; i <= n; i++)
		res *= i;
	return res;
}

// Driver code
int main()
{
	int num = 5;
	cout << "Factorial of "
		<< num << " is "
		<< factorial(num) << endl;
	return 0;
}

// This code is contributed by Shivi_Aggarwal

C program for factorial of a number

#include <stdio.h>

// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
	int res = 1, i;
	for (i = 2; i <= n; i++)
		res *= i;
	return res;
}

int main()
{
	int num = 5;
	printf(
		"Factorial of %d is %d", num, factorial(num));
	return 0;
}

Java program for factorial of a number

// Java program to find factorial of given number
class Test {
	// Method to find factorial of the given number
	static int factorial(int n)
	{
		int res = 1, i;
		for (i = 2; i <= n; i++)
			res *= i;
		return res;
	}

	// Driver method
	public static void main(String[] args)
	{
		int num = 5;
		System.out.println(
			"Factorial of " + num
			+ " is " + factorial(5));
	}
}

Python 3 program for factorial of a number

# Python 3 program to find
# factorial of given number

# Function to find factorial of given number
def factorial(n):
	
	res = 1
	
	for i in range(2, n+1):
		res *= i
	return res

# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))

# This code is contributed by Smitha Dinesh Semwal

C# program for factorial of a number

// C# program to find
// factorial of given number
using System;

class Test {
	// Method to find factorial
	// of given number
	static int factorial(int n)
	{
		int res = 1, i;

		for (i = 2; i <= n; i++)
			res *= i;
		return res;
	}

	// Driver method
	public static void Main()
	{
		int num = 5;
		Console.WriteLine(
			"Factorial of " + num
			+ " is " + factorial(5));
	}
}

// This code is contributed by vt_m

PHP program for factorial of a number

<?php

// function to find factorial
// of given number
function factorial( $n)
{
	$res = 1; $i;
	for ($i = 2; $i <= $n; $i++)
		$res *= $i;
	return $res;
}

// Driver Code
$num = 5;
echo "Factorial of ", $num, " is ",
					factorial($num);

// This code is contributed
// by anuj_67.
?>

Output :

Factorial of 5 is 120

Using While Loop:

C Program for factorial of a number

// C program for factorial of a number
#include <stdio.h>

// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
	if(n == 0)
		return 1;
	int i = n, fact = 1;
	while (n / i != n) {
		fact = fact * i;
		i--;
	}
	return fact;
}

int main()
{
	int num = 5;
	printf("Factorial of %d is %d", num, factorial(num));
	return 0;
}

C++ Program for factorial of a number

// C++ program for factorial of a number
#include <iostream>
using namespace std;

// function to find factorial of given
// number using while loop
unsigned int factorial(unsigned int n)
{
	if(n == 0)
		return 1;
	int i = n, fact = 1;
	while (n / i != n) {
		fact = fact * i;
		i--;
	}
	return fact;
}

// Driver code
int main()
{
	int num = 5;
	cout << "Factorial of "
		<< num << " is "
		<< factorial(num) << endl;
	return 0;
}
// This code is contributed by Shivi_Aggarwal

Java Program for factorial of a number

// Java program to find factorial of given number
class Test {
	// Method to find factorial of the given number
	static int factorial(int n)
	{
		if(n == 0)
		return 1;
		int i = n, fact = 1;
		while (n / i != n) {
			fact = fact * i;
			i--;
		}
		return fact;
	}

	// Driver method
	public static void main(String[] args)
	{
		int num = 5;
		System.out.println(
			"Factorial of " + num
			+ " is " + factorial(5));
	}
}

Python Program for factorial of a number

# Python 3 program to find
# factorial of given number

# Function to find factorial of given number
def factorial(n):
	if(n == 0):
	return 1
	i = n
	fact = 1
	
	while(n / i != n):
		fact = fact * i
		i -= 1
		
	return fact

# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))

# This code is contributed by Smitha Dinesh Semwal

C# Program for factorial of a number

// C# program to find
// factorial of given number
using System;

class Test {
	// Method to find factorial
	// of given number
	static int factorial(int n)
	{
		if(n == 0)
			return 1;
		int i = n, fact = 1;
		while (n / i != n) {
			fact = fact * i;
			i--;
		}
		return fact;
	}

	// Driver method
	public static void Main()
	{
		int num = 5;
		Console.WriteLine(
			"Factorial of " + num
			+ " is " + factorial(5));
	}
}

Output:

Factorial of 5 is 120

So time complexity of above iterative is O(n).

Please share this article if you will get any helpful and for improve this article then WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter