# Factorial of a Number using Loop in C++

## Factorial of a Number using Loop in C++

In this article, I am going to discuss the Program to Print Factorial of a Number using Loop in C++ with Examples. Please read our previous articles, where we discussed the Sum of N natural numbers using loop in C++ with Examples.

##### Factorial of a Number:

Let us understand the procedure then the flowchart and then the program. If we have a number â€˜n = 6â€™, integer number, it should not be a decimal number. Factorial of that number means the product of first â€˜nâ€™ natural numbers that are

That is factorial. So, 6! = 720. Here, we are multiplying numbers i.e. 1 * 2 * â€¦ up to that number which we want factorial. Multiplication is a repeating step, when it is repeating then we have to do it by using a loop. So how it is repeated? To understand that we have data below in the form of a table. Let us explain these step by step.

1. First, we multiply â€˜1â€™ with â€˜1â€™, because there is no other number, so the result is â€˜1â€™. If we multiply with â€˜0â€™ then the result of the factorial will be zero.
2. Now, multiply â€˜2â€™ with the previous result that is â€˜1 * 2 = 2â€™.
3. Now, again multiply â€˜3â€™ to the result of previous multiplication â€˜2 * 3 = 6â€™.
4. Multiply â€˜4â€™ with the previous result that is â€˜6 * 4 = 24â€™.
5. Multiply â€˜5â€™ with the result of the previous addition that is â€™24 * 5 = 120â€™.
6. Multiply â€˜6â€™ to the previous result that is â€™120 * 6 = 720â€™.

The result of â€˜6!â€™ is 720. In this way, we can calculate the factorial of any number. So â€˜iâ€™ is multiplied with the value in every step. And the result is stored in that value. So let us call this value â€˜factâ€™. And in each step, we are multiplying this â€˜factâ€™ value with â€˜iâ€™ as â€˜fact = fact * iâ€™. So, we are modifying â€˜factâ€™ in each and every step. So initially the value of â€˜factâ€™ is â€˜1â€™. Now let us look at the flowchart:

##### Factorial of a Number Flowchart:

Let us study that flow chart. First, we take a number from the user. Then we should initialize the counter to â€˜1â€™ as well as our â€˜factâ€™ variable to â€˜1â€™, we will not initialize â€˜factâ€™ to â€˜0â€™ as the result will be 0. Both â€˜iâ€™ and â€˜factâ€™ will be modified in every iteration of the loop.

Then up to where we want to find the factorial, let’s take that number be â€˜nâ€™. So, we will check the condition as â€˜if (i <= n)â€™. If our counter is equal to the given number then the loop will be exit.

If the condition will true then modify the â€˜factâ€™ variable as â€˜fact = fact * iâ€™ and increment the â€˜iâ€™ variable by 1. After this step, control goes back to the condition and check if the condition is true then again modify the â€˜sumâ€™ and â€˜iâ€™ variables and it will continue until the condition become false. If the condition will false, then simply stop the loop. Now let us look at the program.

##### Program to find Factorial of a number using loop in C++:
```#include <iostream>
using namespace std;
int main()
{
int n, fact = 1;
cout << "Enter number:" << endl;
cin >> n;
cout << endl;
for (int i = 1; i <= n; i++)
{
fact *= i;
}
cout << "Factorial of " << n << " is " << fact << endl;
return 0;
}
```