# C program to find factorial of a number In this article, we’ll learn ways to find the factorial of a number in C. The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, 4! = 4*3*2*1 = 24.

There are many ways in C to find the factorial of a number, some of them are mentioned below.

## Program: C program to find factorial of a number using for loop

The following code example uses for loop to find the factorial of a number entered by the user and then prints the factorial.

```#include<stdio.h>
int main()
{
long int n,fact=1,i;
printf("Find the factorial of :-> ");
scanf("%li",&n);
for(i=1;i<=n;i++)
fact *= i;
printf("Factorial of %li = %li\n",n,fact);
return 0;
}```

### Output: Factorial program in C sample output

``````Find the factorial of :-> 6
Factorial of 6 = 720``````

The same thing can also be done using a function. It uses the same algorithm, but the algorithm to find the factorial is written separately inside a function. Below is the code for the same.

## Program: C program to find the factorial of a number using function

```#include<stdio.h>
long int factorial(long int);  //function prototype
int main()
{
long int n,i;
printf("Enter a number :-> ");
scanf("%li",&n);
printf("Factorial of %li = %li\n",n,factorial(n));  //function calling here
return 0;
}
long int factorial(long int num){   //function definition
long int fact = 1;
for(int i=1;i<=num;i++)
fact *= i;
return fact;
}```

Factorial of a number can also be found using recursion. To implement the idea of calculating the factorial using recursion, you must have the understanding of recursion in C. A recursive function is a function that calls itself.

## Program: C program to find the factorial of a number using recursion

``` #include<stdio.h>
long int fact(long int);      //function declaration or prototype
int main()
{
long int num, result;           //for smaller values use int.
printf("Enter a number: ");
scanf("%ld", &num);
if (num < 0)
{
printf("Try again!!\n");
}
else
{
result = fact(num);
printf("The Factorial of %ld is %ld\n", num, result);
}
return 0;
}
long int fact(long int num)         //function definition
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * fact(num - 1));
}
}```

Output:

``````Enter a number: 5
The Factorial of 5 is 120``````

## Program: C program to find the factorial of a number using while loop

If you understand the working of the factorial and its algorithm, you can find it using different ways. The following C program example finds the factorial of a number entered by the user using while loop.

```#include<stdio.h>
int main()
{
long int n,fact=1,i = 1;
printf("Enter an integer :-> ");
scanf("%li",&n);
while(i<=n){
fact *= i;
i++;
}
printf("Factorial of %li = %li\n",n,fact);
return 0;
}```

Output:

``````Enter an integer :-> 10
Factorial of 10 = 3628800``````

There is one more interesting problem related to factorial in C.

## Program: C program to print the sum of factorial series

The following C program takes the limit of the factorial series as input and prints the pattern : 1!+2!+3!+4!+…n! .

```#include<stdio.h>
int main()
{
int n,i,j,fact=1,sum=0;
printf("Enter the limit of the factorial series: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
fact*=j;
sum+=fact;
fact=1;
}
printf("The sum of the factorial series of %d terms is : %d\n",n,sum);
return 0;
}```

Output: Factorial program in C

``````Enter the limit of the factorial series: 5
The sum of the factorial series of 5 terms is : 153``````

In this article, we learned about the factorial program in C. For more programming articles and code snippets, visit programming articles. 