Home C++ Calculating Factorial in C++

Calculating Factorial in C++

We will use C++ to write programs to calculate the factorial of a number. We will take the number as an input from the user and then print the factorial of that number in the console. We will use two methods to calculate the factorial of the number, the first is by performing iteration with the help of loops, and the second is by using recursion.

To follow this tutorial, it is recommended to install a compiler in your local environment and run the c++ code that will be discussed in his tutorial. Many Linux system comes preinstalled with g++ compiler, and I will use the same in this tutorial. You can also use an IDE like the open-source Visual Studio Code provided by Microsoft, a cross-platform IDE for writing code efficiently. It also provides many functionalities that increase the speed of coding.

Calculating Factorial in C++

Factorial of a natural number n is the product of all the natural numbers that are lesser than n, including n. The general formula for calculating the factorial for a number n is:-

Fact(n) = n x (n-1) x (n-2) x … x 3 x 2 x 1

This formula can also be written as Fact(n) = n x Fact(n-1)

For example, if we want to calculate the factorial of 5 then it will be-

Fact(5) = 5 x 4 x 3 x 2 x 1 = 120,

As a result we get the factorial of 5 is 120.

Note: The factorial of zero and one is the same i.e 1. So, Fact(0) = Fact(1) = 1

We have learned how to calculate the factorial manually using multiplication. Now let us write C++ programs to implement the same functionality.

Calculating Factorial using loops

Here we will see how to calculate the factorial of a number using loops. We will use the for loop of C++ to iterate up to the given number and multiply each number with the product of the previous two numbers. The below code shows how we can calculate the factorial of a number.

// including the required header files
#include <iostream>
using namespace std;
int main()
{
    int p = 1, num, i;
    cout << "[+] Enter the number : ";
    cin >> num; // inputting the number for which we need to calculate the factorial
    // calculating the factorial
    for (i = num; i > 0; i--)
    {
        p = p * i;
    }
    // displaying the factorial of the number to the console
    cout << "[+] The factorial of " << num << " is " << p << endl;
    return 0;
}

We first include all the required header files in the above code, and C++ boilerplate code has been added. In the main function, we created three variables viz. p, num, i to store the factorial, the input of the user, and the loop variable, respectively. Next, we prompt the user to enter the number and then store the number in the num variable. Next, we use the for loop of C++ to iterate from the number and decrement to 1 and multiply each number with the product of the previous two numbers and store it in the p variable. After the loop completion, we print the result of multiplication, i.e., the factorial, to the console. On running the above code, we will get the output as shown in the below image.

A simple C++ program to calculate the factorial of a number using for loops only
A simple C++ program to calculate the factorial of a number using for loops only

Calculating Factorial with Recursion

We have seen how we can calculate the factorial of a number by just using the for loops, but there is also an easier way to perform the same operation, i.e., by using recursion. Recursion is the process in which a function is called in its own definition. Recursion is a trendy method in algorithm design, and it can be used to write larger programs in a simple way. But recursion can’t be used everywhere as it sometimes complexes the code and makes it harder to understand and write. So let us see how recursion helps us to calculate the factorial of a number.

Let us implement recursion to calculate the factorial of a number using the C++ programming language. See the below code for illustration.

// including the required header files
#include <iostream>
using namespace std;
// creating the fact() function where we will perform recursion
int fact(int n)
{
    if (n <= 1)
    {
        return n;
    }
    // performing recursion
    return n * fact(n - 1);
}

int main()
{
    int p = 1, num, i;
    cout << "[+] Enter the number : ";
    cin >> num;
    //calling the fact() function and displaying the return value of it
    cout << "[+] The factorial of " << num << " is " << fact(num) << endl;
    return 0;
}

In the above code, we first include the required header files and the basic boilerplate of the C++ language. Next, we create a fact() function that accepts an integer as an argument and then returns the factorial of the number. This function is used to implement recursion and to calculate the factorial of the number. In the main() function, we first created the required variables as done in the previous code. Then we prompt the user to enter the number for which we will calculate the factorial. Next, we call the fact() function by passing the number and then displaying the function’s return value. As a result, we will get the factorial of the number displayed in the console as shown in the below image.

A simple C++ program to calculate the factorial of a number using recursion
A simple C++ program to calculate the factorial of a number using recursion

Conclusion

In this tutorial, we have seen how to calculate the factorial of a number. We use two methods to operate, i.e., by using the simple for loop and performing recursion. You may also want to see our tutorial on printing spiral matrices using C++.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More