Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

C++ Program to Find Factorial



In this article, we'll show you how to write a C++ program to find the factorial of a number. The factorial of a number is the result of multiplying all the positive integers from 1 to that number. It is written as n! and is commonly used in mathematics and programming.

Let's understand this with a few examples:

//Example 1
Input:
5
The factorial of 5 is: 5 * 4 * 3 * 2 * 1 = 120
Output:
120

//Example 2
Input:
6
The factorial of 6 is: 6 * 5 * 4 * 3 * 2 * 1 = 720
Output:
720

We can calculate the factorial of a number in the following ways:

Calculating FactorialUsing Loop (Iterative Approach)

In this approach, we use a loop to multiply numbers from 1 to n. It's a simple and direct way to calculate the factorial.

Example

Here's the C++ program where we initialize fa = 1 and use a for loop to multiply it by each number from 1 to n.

#include <iostream>
using namespace std;

int main() {
    int n = 5;         //number to calculate the factorial
    int fa = 1;
    // for loop for calculation
    for (int i = 1; i <= n; ++i) {
        fa *= i;
    }

    cout << "Factorial of " << n << " using loop is: " << fa << endl;
    return 0;
}

The output of the above program shows the factorial of 5 calculated using a loop.

Factorial of 5 using loop is: 120

Time Complexity: O(n), because the loop runs n times.

Space Complexity: O(1).

Calculating Factorial Using Recursion

In this approach, we use recursion to solve the problem by calling the same function repeatedly with smaller values, until we reach the base case.

Example

In the example below, we define the factorial() function which calls itself with n - 1 until it reaches n == 0.

#include <iostream>
using namespace std;

int factorial(int n) {
    if (n == 0)  // Base case: when n is 0, return 1
        return 1;
    return n * factorial(n - 1);  // multiply n by the result of factorial(n - 1)
}

int main() {
    int n = 5;
    cout << "Factorial of " << n << " using recursion is: " << factorial(n) << endl;
    return 0;
}

Once you run the above code, you will get the following output:

Factorial of 5 using recursion is: 120

Time Complexity: O(n) because the function calls itself n times.

Space Complexity: O(n) due to the recursive call stack, which grows to a depth of n.

Calculating Factorial Using Memoization (Recursive with Storage)

In this approach, we improve the regular recursive method by saving the results we've already calculated. This helps us avoid doing the same work over and over, especially when calculating factorials for different numbers or in bigger programs.

Example

In this example, we use a vector<int> dp[] to store the factorials we've already calculated, so we can use them again if needed.

#include <iostream>
#include <vector>
using namespace std;

int factorialMemo(int n, vector<int>& dp) {
    if (n == 0)  // Base case: factorial of 0 is 1
        return 1;

    if (dp[n] != -1)  // If result is already computed, return it
        return dp[n];

    // Store the computed factorial in dp[n]
    return dp[n] = n * factorialMemo(n - 1, dp);
}

int main() {
    int n = 5;
    vector<int> dp(n + 1, -1);  // Initialize dp array with -1 (indicating uncalculated values)
    cout << "Factorial of " << n << " using memoization is: " << factorialMemo(n, dp) << endl;
    return 0;
}

The output below shows the factorial of 5 is calculated using memoization.

Factorial of 5 using memoization is: 120

Time Complexity: O(n), because we calculate each factorial once and reuse it.

Space Complexity: O(n), because we store results in the dp array and use the call stack for recursion.

Updated on: 2025-05-09T15:30:29+05:30

23K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements