In Mathematics, the factorial of a non-negative integer is the product of all positive integers less than or equal to that non-negative integer whose factorial is being taken. The symbol of factorial is exclamation mark (!). For example:

5! = 5 x 4 x 3 x 2 x 1 = 120

12! = 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 479001600

In C++, finding the factorial is very easy. All you need is to firstly just understand the concept of factorial and then implement your concept in coding. Here is the simple basic code to find the factorial of any number.

#include

using namespace std;

int main()

{

// Variable Declaration

int counter, num, fact = 1;

// Get Input

cout<<"Enter the Number to find its Factorial: ";

cin>>num;

//'for' Loop

for (int counter = num; counter >= 1; counter--)

{

fact = fact * counter;

}

cout<cout<<"Factorial of "< return 0;

}

### Explanation:

In this program I've used the for loop to find the factorial of any number given by the user. For example, the user enters a number '5'. This number will be stored in the the variable 'num' and then the 'for' loop body starts. For loop will execute the loop in reverse order from 5 to 1. It will multiply the value of counter with variable fact and the store its value in fact. e.g:

*1=1*5*

*5=5*4*

*20=20*3*

*60=60*2*

*120=120*1*

Finally the program will output the last value stored in the variable fact. According to the given example, the program will show 120 on the output screen because the factorial of 5 is 120. You can also see the output screen given below.

Note: Only a member of this blog may post a comment.