C Program to find the factorial of a number

The following C program find the factorial of a number:

Expected Output

1st run:

2nd run:

How it works:

Factorial of a number n is defined as:

For example:

The following table demonstrates what happens at each iteration of the loop (assuming the n = 5):

Iteration fact i
After 1st iteration fact = 1 * 5 = 5 5
After 2nd iteration fact = 5 * 4 = 20 4
After 3rd iteration fact = 20 * 3 = 60 3
After 4th iteration fact = 60 * 2 = 120 2
After 5th iteration fact = 120 * 1 = 120 1

Recommended Reading:

Leave a Comment

%d bloggers like this: