# Factorial Program in C

In mathematics, the factorial of a positive integer n, denoted by n! It is the product of all positive integers less than or equal to n. For example:- The factorial of 4= 4! = 4*3*2*1 or 1*2*3*4 = 24 Here we will write the Factorial program in C programming language.

Factorial (n) = 1 * 2 * …. * (n-1) * n
Or,
Factorial (n) = n * (n-1) * ….. * 2 * 1

The factorial of a negative number doesn’t exist. The value of 0! is 1, according to the convention for an empty product.

## Factorial of a Number in C using For loop

The for loop in C programming language is a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

The for loop contains the initialization, update, and test_condition in one statement, so it makes for very readable code. All the control steps, initialization, end-of-loop test, and updating are done in one place.

We can use the increment operator to find the factorial of a number. So, using increment operator the 4! is evaluated as 1*2*3*4

Factorial program using for loop and increment (++) operator

`````` #include<stdio.h>
int main()
{
int number, i, fact=1;

printf("Enter number: ");
scanf("%d", &number);

for(i=1; i<=number; i++)
{
fact *= i; // fact = fact * i
}

printf("%d! = %d", number, fact);

return 0;
}``````

Output:-

Enter number: 5
5! = 120

Factorial of a Number Using for loop and decrement `(--) `operator

We can use the decrement operator to find the factorial of a number. So, using the decrement operator for 4! = 4*3*2*1

`````` #include<stdio.h>
int main()
{
int number, i, fact=1;

printf("Enter number:");
scanf("%d",&number);

for(i=number; i>0; i--)
{
fact *= i; // fact = fact * i
}

printf("%d! = %d", number, fact);

return 0;
}``````

In this case, initialize the variable `i` with the number and use decrement operation as update expression until the number is greater than zero.

## Factorial program in C using while loop

We can also write the factorial program in C using while loop. While loop in C is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

`````` #include<stdio.h>
int main()
{
int number, i=1, fact=1;

printf("Enter number:");
scanf("%d",&number);

while(i<=number)
{
fact *= i;
i++;
}

printf("%d! = %d", number, fact);

return 0;
}``````

## Using the do-while loop

The do-while loop in C is very closely related to the while loop. The do keyword is placed on a line of code at the top of the loop. A block of statements follows it with a test expression after the keyword while, at the bottom of the loop.

It is a post-test loop. The post-test loop executes at least, only then control expression is tested. If the expression is true, the loop repeats. When the expression is false then the loop terminates.

We can also use the do-while loop to write the factorial program in C.

`````` #include<stdio.h>
int main()
{
int number, i=1, fact=1;

printf("Enter number: ");
scanf("%d",&number);

do
{
fact *= i;
i++;
} while(i<=number);

printf("%d! = %d", number, fact);

return 0;
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!