Before we begin to look various ways of creating a factorial program in C languages, we should learn about what does factorial means?

**Factorial**: The Factorial of a specified number refers to the product of all given series of consecutive whole numbers beginning with 1 and ending with the specified number

We use the “!” to represent factorial

**Example**:

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

**Recursion**: In C programming language, if **a function calls itself over and over again** then that function is known as **Recursive Function**. The **process** of function **calling itself repeatedly** is known as **Recursion**.

### Factorial program in c using recursion

```
#include<stdio.h>
#include<conio.h>
void main()
{
int num,f;
clrscr();
printf("\n Enter the number: ");
scanf("%d",&num);
f=factorial(num);
printf("\n The factorial of the number %d is %d",num,f);
getch();
}
int factorial(int number)
{
if(number==0 || number==1)
return 1;
else
return(number * fact(number-1));
}
```

Understanding the code:

- At First, the compiler reads the number to find the factorial of that number from the user(using scanf for this)
- Then we are using the recursive function to calculate the factorial value and returns the factorial value to the main function.
- Finally, the factorial value of the given number is printed.

The **output** of the above code will be as below

```
Enter the number: 5
The factorial of the number 5 is 120
```

You can try it online

### Factorial program in c using for loop

```
#include<stdio.h>
int main(){
int i,f=1, num;
printf("Enter a number: ");
scanf("%d", &num);
for(i=1;i<=num;i++)
f = f * i;
printf("%d! = %d\n", num, f);
return 0;
}
```

Executing the above code will give output as below

You can try it online here

### C program to print Factorial series in a given range

```
#include<stdio.h>
#include<conio.h>
int main(){
long f=1;
int i,num,min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
getch();
printf("Enter the maximum range: ");
scanf("%d",&max);
getch();
printf("Factorial series in given range: ");
for(num=min;num<=max;num++){
f=1;
for(i=1;i<=num;i++)
f=f*i;
printf("%ld ",f);
}
return 0;
}
```

Executing the above code will give output as below

You can try this online

### Factorial program in c using while loop

```
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
long fact=1;
clrscr();
printf("\n Enter Any Number: ");
scanf("%d",&num);
if(num<=0)
printf("\n Invalid Number! ");
else
{
fact=num;
i=1;
while(i<num)
{
fact=fact*i;
i++;
}
printf("\n Factorial = %ld",fact);
}
getch();
}
```

In the above program, we are asking the user for a factorial number, then we check if the number if valid(if it is greater than 0), if yes, while loop is executed until i=1 is less than the number.

You can try this online

That's it, there are several ways to calculate the factorial of any number, but it depends on our need, widely used methods are using recursion & using while loop.