# Factorial Program in C

Write a Program to Find the Factorial of a number in C.

Example,

`Input: 5Output: 120`

Factorial can be calculated in 2 ways

1. Factorial using Loop (Iteratively)
2. Factorial using recursion (recursively)

## Factorial Program in C Using Loop (Iterative method)

We can use for-loop, while-loop or do-while loop.

### Factorial Program in C using for-loop

``````#include<stdio.h>

int main(){

int n, fact, i;
printf("Enter Number: ");
scanf("%d",&n);

fact = 1;

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

printf("Factorial of %d is: %d ", n, fact);

return 0;
}``````

### Factorial Program in C using while-loop

``````#include<stdio.h>

int main(){

int n, fact, i;
printf("Enter Number: ");
scanf("%d",&n);

fact = 1;

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

printf("Factorial of %d is: %d ", n, fact);

return 0;
}``````

### Factorial Program in C using do-while loop

``````#include<stdio.h>

int main(){

int n, fact, i;
printf("Enter Number: ");
scanf("%d",&n);

fact = 1;

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

printf("Factorial of %d is: %d ", n, fact);

return 0;
}``````

## Factorial Program in C using Recursion (Recursively)

``````#include<stdio.h>

int fact(int n){
if(n==0||n==1)
return 1;
else
return n*fact(n-1);
}

int main(){

int n, i;
printf("Enter Number: ");
scanf("%d",&n);

printf("Factorial of %d is: %d ", n, fact(n));

return 0;
}``````

#### Output

What to study next?