The factorial of a number is a mathematical operation commonly used in permutations, combinations, probability, and various mathematical computations.
- Defined only for non-negative integers.
- The factorial of 0 is defined as 1.
- Can be calculated using iterative or recursive approaches.

Methods to Find the Factorial of a Number
The following approaches demonstrate different ways to calculate the factorial of a number in C++.
Iterative Approach
The factorial of a number can be calculated iteratively by multiplying all integers from 1 to n. Iterative solutions are generally preferred over recursion because they avoid function call overhead and use constant extra space.
Using a For loop
#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;
}
Output
Factorial of 5 is 120
Explanation
- The variable res stores the factorial value.
- The for loop multiplies res by every number from 2 to n.
- After the loop finishes, res contains the factorial of the given number.
Complexity Analysis
Time complexity: O(n)
Auxiliary Space: O(1)
Using a While loop
The following program calculates the factorial of a number using a while loop by repeatedly multiplying the current value and decrementing the counter until all numbers are processed.
#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 (i > 1) {
fact *= i;
i--;
}
return fact;
}
// Driver code
int main()
{
int num = 5;
cout << "Factorial of " << num << " is "
<< factorial(num) << endl;
return 0;
}
Output
Factorial of 5 is 120
Explanation
- The variable fact stores the factorial value, while i is used as a counter.
- The while loop repeatedly multiplies fact by the current value of i.
- The value of i is decremented in each iteration until all numbers are processed.
- The final value of fact represents the factorial of the given number.
Complexity Analysis
Time complexity: O(n)
Auxiliary Space: O(1)
Recursive Approach
A factorial can also be calculated recursively using the mathematical relationship between a number and the factorial of its predecessor.
Formula
n! = n × (n−1)!
n! = 1 if n = 0 or n = 1
Using Recursion
#include <iostream>
using namespace std;
// Function to find factorial of
// given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver code
int main()
{
int num = 5;
cout << "Factorial of " << num << " is "
<< factorial(num) << endl;
return 0;
}
Output
Factorial of 5 is 120
Explanation
- The function calls itself with n - 1.
- The recursion continues until n becomes 0.
- The recursive calls are then resolved to compute the factorial value.
Complexity Analysis
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Using Ternary Operator
The factorial can also be computed recursively using the ternary operator, which combines the base case and recursive case into a single statement.
#include <iostream>
using namespace std;
int factorial(int n)
{
// Single line to find factorial
return ((n == 1 || n == 0) ? 1 :
n * factorial(n - 1));
}
// Driver Code
int main()
{
int num = 5;
cout << "Factorial of " <<
num << " is "<<
factorial(num);
return 0;
}
Output
Factorial of 5 is 120
Explanation
- The ternary operator checks whether n is 0 or 1.
- If true, it returns 1; otherwise, it returns n * factorial(n - 1).
- This approach produces the same result as recursion but in a more compact form.
Complexity Analysis
Time complexity: O(n), for recursion.
Auxiliary space: O(n), for recursion call stack.
Using Call By Reference
In this approach, the factorial value is computed inside a function and returned through a reference parameter instead of the function's return value.
#include<iostream>
using namespace std;
// Function to find factorial
void factorial(int n, int *f)
{
int i;
for(i = n; i >= 1; i--)
*f = (*f) * i;
}
// Driver code
int main()
{
int num = 5, fact = 1;
factorial(num, &fact);
cout << "Factorial of " <<
num << " is "<<
fact;
return 0;
}
Output
Factorial of 5 is 120
Explanation
- The variable fact is passed by reference to the function.
- The loop multiplies all numbers from n down to 1.
- The computed factorial is stored directly in the referenced variable.
- No value needs to be returned from the function.
Complexity Analysis
- Time Complexity: O(n)
- Auxiliary Space: O(1)