Factorial is used in many areas of mathematics but mainly used in permutation and combination. Factorial is the product of the all positive number from 1 to n (user entered number). In simple word we can say that factorial of n would be 1*2*3*…..*n.
Note: There is no factorial exist for the negative number and value of !0 is 1.



Factorial of positive number would be:

!n  = n * !(n-1)

For example,

!5 =  5*4*3*2*1*!0  =  120.

Some factorials of numbers

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

 

Here I am describing few method to calculate the factorial of a positive number in c .I hope you are familiar with while and for loop in c.

 Factorial of a number in c using the for loop

The below program takes a positive integer number from the user and computes its factorial using the for loop.

OutPut

When the user Enters the negative number.

factorial

When the user enters the positive number.

factorial

Factorial of a number using the recursive method.

A function is called recursive, if it is called by itself.

Explaination:
In a program we know,
iCalculateFactorial(n) = n * iCalculateFactorial(n – 1);
so if I assume here n = 3
iCalculateFactorial(3) = 3 * iCalculateFactorial(2)
iCalculateFactorial(2) = 2* iCalculateFactorial(1)
iCalculateFactorial(1) = 1
So final result will be:
iCalculateFactorial(3) = 3*2*1 = 6
Note: There should be a termination point in a recursive function.

Factorial of a number in c using the while loop




Calculate the factorial using the look-up table

If you need the factorial of the small number then look up table is the best option because it is fast as compared to iteration and recursion method.
In below program, I am creating a look-up table which contains the factorial of-of 0 to 10;

Calculate the factorial using a function