Calculate Factorial of a Number in C++

  1. Use Iterative Method to Calculate Factorial of a Number in C++
  2. Use Recursive Method to Calculate Factorial of a Number

This article will explain several methods of how to calculate the factorial of a number in C++.

Use Iterative Method to Calculate Factorial of a Number in C++

The factorial of the number is calculated by multiplying all whole numbers starting from one and including the given number. Note that the straightforward algorithm is to use iteration using one of the loop statements. In the following example code, we implemented a while loop which accumulates the multiplied values in a variable and returns the resulting integer by value to the caller. Note that the loop decrements the number in each cycle, and the n-- expression is utilized to store the value before it is decremented.

#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::vector;

int factorial1(int n)
{
    int ret = 1;
    while (n > 1)
        ret *= n--;
    return ret;
}

template<typename T>
void printVector(vector<T> &vec)
{
    for (const auto &item : vec) {
        cout << item << ", ";
    }
    cout << endl;
}

int main(){
    vector<int> vec = {1,2,3,4,5,6,7,8,9,10};

    printVector(vec);

    for (const auto &item : vec) {
        cout << factorial1(item) << ", ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,

Use Recursive Method to Calculate Factorial of a Number

Another solution is to employ recursive function calls for factorial calculation. Recursion is the feature of the function to call itself from its body. The main part of the recursive function is to define a condition that ensures that it returns to the caller and not get dragged into infinite loop-style behavior. In this case, we specify the if condition to denote the state when the recursive call should be made. Otherwise, the function should return 1.

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::vector;

int factorial2(int n)
{
    if (n > 1)
        return factorial2(n-1) * n;
    return 1;
}

template<typename T>
void printVector(vector<T> &vec)
{
    for (const auto &item : vec) {
        cout << item << ", ";
    }
    cout << endl;
}

int main(){
    vector<int> vec = {1,2,3,4,5,6,7,8,9,10};

    printVector(vec);

    for (const auto &item : vec) {
        cout << factorial2(item) << ", ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,

Alternatively, we can merge the recursive function in a one-liner implementation where the ?: expression evaluates if the given number equals 0 or 1, in which case the function returns the 1. If the condition is false, then the recursive call is made, and it continues with the new function stack frame until the condition is true. The example code outputs the factorial of each element in the declared vector.

#include <iostream>
#include <vector>

using std::cout; using std::endl;
using std::string; using std::cin;
using std::vector;

int factorial3(int n)
{
    return (n==1 || n==0) ? 1 : n * factorial3(n - 1);
}

template<typename T>
void printVector(vector<T> &vec)
{
    for (const auto &item : vec) {
        cout << item << ", ";
    }
    cout << endl;
}

int main(){
    vector<int> vec = {1,2,3,4,5,6,7,8,9,10};

    printVector(vec);

    for (const auto &item : vec) {
        cout << factorial3(item) << ", ";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800,
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C++ Math

  • Generate Fibonacci Numbers in C++
  • Raise Number to Power in C++