Factorial of a non-negative integer, is multiplication of all positive integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.
A factorial is represented by a number and a ” ! ” mark at the end. It is widely used in permutations and combination to calculate the total possible outcomes. A French mathematician Christian Kramp firstly used the exclamation.
Recursive Solution: Factorial can be calculated using following recursive formula.
printf("Factorial of %d is %d", num, factorial(num));
return0;
}
Java
// Java program to find factorial of given number
classTest {
// method to find factorial of given number
staticintfactorial(intn)
{
if(n == 0)
return1;
returnn * factorial(n - 1);
}
// Driver method
publicstaticvoidmain(String[] args)
{
intnum = 5;
System.out.println("Factorial of "+ num
+ " is "+ factorial(5));
}
}
Python3
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
deffactorial(n):
ifn ==0:
return1
returnn *factorial(n-1)
# Driver Code
num =5;
print("Factorial of", num, "is",
factorial(num))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program to find factorial
// of given number
usingSystem;
classTest {
// method to find factorial
// of given number
staticintfactorial(intn)
{
if(n == 0)
return1;
returnn * factorial(n - 1);
}
// Driver method
publicstaticvoidMain()
{
intnum = 5;
Console.WriteLine("Factorial of "
+ num + " is "+ factorial(5));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to find factorial
// of given number
// function to find factorial
// of given number
functionfactorial($n)
{
if($n== 0)
return1;
return$n* factorial($n- 1);
}
// Driver Code
$num= 5;
echo"Factorial of ", $num, " is ", factorial($num);
// This code is contributed by m_kit
?>
Javascript
<script>
// Javascript to find factorial
// of given number
// function to find factorial
// of given number
functionfactorial(n) {
if(n == 0) return1;
returnn * factorial(n - 1);
}
// Driver Code
let num = 5;
document.write("Factorial of "+ num + " is "+ factorial(num));
// This code is contributed by Saurabh Jaiswal
</script>
Output:
Factorial of 5 is 120
Time Complexity: O(n)
Iterative Solution: Factorial can also be calculated iteratively as recursion can be costly for large numbers. Here we have shown the iterative approach using both for and while loop. Using For loop
C++
// C++ program for factorial of a number
#include <iostream>
usingnamespacestd;
// function to find factorial of given number
unsigned intfactorial(unsigned intn)
{
intres = 1, i;
for(i = 2; i <= n; i++)
res *= i;
returnres;
}
// Driver code
intmain()
{
intnum = 5;
cout << "Factorial of "
<< num << " is "
<< factorial(num) << endl;
return0;
}
// This code is contributed by Shivi_Aggarwal
C
#include <stdio.h>
// function to find factorial of given number
unsigned intfactorial(unsigned intn)
{
intres = 1, i;
for(i = 2; i <= n; i++)
res *= i;
returnres;
}
intmain()
{
intnum = 5;
printf(
"Factorial of %d is %d", num, factorial(num));
return0;
}
Java
// Java program to find factorial of given number
classTest {
// Method to find factorial of the given number
staticintfactorial(intn)
{
intres = 1, i;
for(i = 2; i <= n; i++)
res *= i;
returnres;
}
// Driver method
publicstaticvoidmain(String[] args)
{
intnum = 5;
System.out.println(
"Factorial of "+ num
+ " is "+ factorial(5));
}
}
Python3
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
deffactorial(n):
res =1
fori inrange(2, n+1):
res *=i
returnres
# Driver Code
num =5;
print("Factorial of", num, "is",
factorial(num))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program to find
// factorial of given number
usingSystem;
classTest {
// Method to find factorial
// of given number
staticintfactorial(intn)
{
intres = 1, i;
for(i = 2; i <= n; i++)
res *= i;
returnres;
}
// Driver method
publicstaticvoidMain()
{
intnum = 5;
Console.WriteLine(
"Factorial of "+ num
+ " is "+ factorial(5));
}
}
// This code is contributed by vt_m
PHP
<?php
// function to find factorial
// of given number
functionfactorial( $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.
?>
Javascript
<script>
// JavaScript program to find factorial of given number
// Method to find factorial of the given number
functionfactorial(n)
{
varres = 1, i;
for(i = 2; i <= n; i++)
res *= i;
returnres;
}
// Driver method
varnum = 5;
document.write("Factorial of "+ num + " is "+ factorial(5));
// This code is contributed by shivanisinghss2110.
</script>
Output :
Factorial of 5 is 120
Time Complexity: O(n)
Using While loop
C
// C program for factorial of a number
#include <stdio.h>
// function to find factorial of given number
unsigned intfactorial(unsigned intn)
{
if(n == 0)
return1;
inti = n, fact = 1;
while(n / i != n) {
fact = fact * i;
i--;
}
returnfact;
}
intmain()
{
intnum = 5;
printf("Factorial of %d is %d", num, factorial(num));
return0;
}
C++
// C++ program for factorial of a number
#include <iostream>
usingnamespacestd;
// function to find factorial of given
// number using while loop
unsigned intfactorial(unsigned intn)
{
if(n == 0)
return1;
inti = n, fact = 1;
while(n / i != n) {
fact = fact * i;
i--;
}
returnfact;
}
// Driver code
intmain()
{
intnum = 5;
cout << "Factorial of "
<< num << " is "
<< factorial(num) << endl;
return0;
}
// This code is contributed by Shivi_Aggarwal
Java
// Java program to find factorial of given number
classTest {
// Method to find factorial of the given number
staticintfactorial(intn)
{
if(n == 0)
return1;
inti = n, fact = 1;
while(n / i != n) {
fact = fact * i;
i--;
}
returnfact;
}
// Driver method
publicstaticvoidmain(String[] args)
{
intnum = 5;
System.out.println(
"Factorial of "+ num
+ " is "+ factorial(5));
}
}
Python3
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
deffactorial(n):
if(n ==0):
return1
i =n
fact =1
while(n /i !=n):
fact =fact *i
i -=1
returnfact
# Driver Code
num =5;
print("Factorial of", num, "is",
factorial(num))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program to find
// factorial of given number
usingSystem;
classTest {
// Method to find factorial
// of given number
staticintfactorial(intn)
{
if(n == 0)
return1;
inti = n, fact = 1;
while(n / i != n) {
fact = fact * i;
i--;
}
returnfact;
}
// Driver method
publicstaticvoidMain()
{
intnum = 5;
Console.WriteLine(
"Factorial of "+ num
+ " is "+ factorial(5));
}
}
Javascript
<script>
// JavaScript Program to implement
// the above approach
// function to find factorial of given
// number using while loop
functionfactorial(n) {
if(n == 0)
return1;
let i = n, fact = 1;
while(Math.floor(n / i) != n) {
fact = fact * i;
i--;
}
returnfact;
}
// Driver code
let num = 5;
document.write("Factorial of "
+ num + " is "
+ factorial(num) + "<br>");
// This code is contributed by Potta Lokesh
</script>
Output :
Factorial of 5 is 120
Time complexity of the above iterative solutions is O(n). One line Solution (Using Ternary operator):
C++
// C++ program to find factorial of given number
#include <iostream>
usingnamespacestd;
intfactorial(intn)
{
// single line to find factorial
return(n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
// Driver Code
intmain()
{
intnum = 5;
cout << "Factorial of "<< num << " is "<< factorial(num);
return0;
}
// This code is contributed by shivanisinghss2110
C
// C++ program to find factorial of given number
#include <stdio.h>
intfactorial(intn)
{
// single line to find factorial
return(n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
// Driver Code
intmain()
{
intnum = 5;
printf("Factorial of %d is %d", num, factorial(num));
return0;
}
// This code is contributed by Rithika palaniswamy.
Java
// Java program to find factorial
// of given number
classFactorial {
intfactorial(intn)
{
// single line to find factorial
return(n == 1|| n == 0) ? 1: n * factorial(n - 1);
}
// Driver Code
publicstaticvoidmain(String args[])
{
Factorial obj = newFactorial();
intnum = 5;
System.out.println(
"Factorial of "+ num
+ " is "+ obj.factorial(num));
}
}
// This code is contributed by Anshika Goyal.
Python3
# Python 3 program to find
# factorial of given number
deffactorial(n):
# single line to find factorial
return1if(n ==1orn ==0) elsen *factorial(n -1)
# Driver Code
num =5
print("Factorial of", num, "is",
factorial(num))
# This code is contributed
# by Smitha Dinesh Semwal.
C#
// C# program to find factorial
// of the given number
usingSystem;
classFactorial {
intfactorial(intn)
{
// single line to find factorial
return(n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
// Driver Code
publicstaticvoidMain()
{
Factorial obj = newFactorial();
intnum = 5;
Console.WriteLine(
"Factorial of "+ num
+ " is "+ obj.factorial(num));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find factorial
// of given number
functionfactorial( $n)
{
// single line to find factorial
return($n== 1 || $n== 0) ? 1:
$n* factorial($n- 1);
}
// Driver Code
$num= 5;
echo"Factorial of ", $num, " is ", factorial($num);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// JavaScript program to find factorial of given number
functionfactorial(n)
{
// single line to find factorial
return(n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
// Driver Code
varnum = 5;
document.write("Factorial of "+num +" is "+ factorial(num));
// This code is contributed by shivanisinghss2110.
</script>
Output:
Factorial of 5 is 120
Problems in writing code of factorial
When the value of n changes increases by 1, the value of the factorial increases by n. So the variable storing the value of factorial should have a large size. Following is the value of n whose factorial can be stored in the respective size.
1. integer –> n<=12
2. long long int –> n<=19
From the above data, we can see that a very small value of n can be calculated because of the faster growth of the factorial function. We can however find the mod value of factorial of larger values by taking mod at each step.
The above solution cause overflow for large numbers. Please refer factorial of large number for a solution that works for large numbers. Please write comments if you find any bug in the above code/algorithm, or find other ways to solve the same problem.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy