Factorial Program in C++

Video Tutorial
FREE
Factorial thumbnail
This video belongs to
Data Structures in C++ Course
13 modules
Certificate
Topics Covered

The factorial of a positive integer n, represented as n! equals the multiplication of all positive integers from n to 1... For instance, the factorial of 5 (5!) equals 120, as 54321=1205*4*3*2*1 = 120. In C++, the factorial calculation can be achieved through iterative loops or recursion, with the understanding that factorials are defined only for non-negative numbers and the factorial of 0 is 1. This concept is crucial in mathematics and computer science, often used in permutations, combinations, and other computational problems.

Factorial Program In C++ Using Loop

Factorial program in C++ can be implemented using the for loop or the while loop; let's understand both ways.

Factorial Program in C++ Using for Loop

We will start the i counter from n and take it to 1. Initially, our answer is 1, and in each iteration, we will multiply the answer with i. We can also print the value of i to understand how the for loop calculates the factorial.

Output :

As we can see in the output, the counter i decreases by 1 in each iteration.

Factorial program in C++ Using while Loop

For using the while loop, we first have to declare the counter i with value as n, and in each iteration, we will decrease the value of the counter i by 1. Rest is similar to the for loop.

Output :

Factorial Program In C++ Using Recursion

Before moving to the implementation of the factorial program in C++ using recursion, let's first understand how we can use recursion here.
Recursion can be applied to a problem if the problem can be solved using the subproblem.
Let's take an example here, 5! = 5x4x3x2x1 and
4! = 4x3x2x1 and
3! = 3x2x1 and
2! = 2x1 and
1! = 1
By observing the above equations, we can see that
5! = 5x4!
4! = 4x3!
3! = 3x2!
2! = 2x1!
1! = 1

As we can see, for the number n, we need the factorial of the number n-1. Hence factorial of number can be implemented using recursion.

For implementing the recursion, we need a base case to stop the recursion call. In the loop, we can see that we were computing till the counter reaches 1. Therefore our base is when the number is 1. And the factorial of the number 1 is 1.
We will create a function fact that will return the result as an integer, and in each recursive call, we will return the multiplication of the number i with the factorial of the number i-1.
Let's look at the factorial program in C++ using recursion.

Output :

In each recursive call, we multiply the number i with the factorial of the number i-1.

Conclusion

  • The factorial of a number n is the product of all the numbers lying in the range n to 1.
  • A factorial program in C++ can be implemented using a loop or recursion. In the case of a loop, the counter iterates over the number in the range n to 1.
  • In the case of recursion, we calculate the result by multiplying the number i with the factorial of the number i-1.