Today we will learn the Factorial Program in c and how to find factorial of a given number using recursion. So before start learning, you should have a little bit of knowledge of factorial of a number. So first of all,

What is Factorial of a Number?

The Factorial of a Number n is the multiplication of all integers less than or equal to n. Factorial of n is represented by n!.

For Example:

Factorial of 5 is 5!=5*4*3*2*1 which is equal to 120.

Note: 5! here ‘!’ is also called as factorial, bang or shriek.

There are so many ways to find factorial of the number we will see it one by one.

1. Program for finding factorial of a number using for loop

This is the simplest way to find factorial of a program. In this program, we will ask the user to enter the number which user want to find factorial of that number.

The factorial of a negative number doesn’t exist so we will apply condition if the user enters a negative number.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i)
{
fact *= i;
 }
printf("Factorial of %%d = %%llu", n, fact);
 }
return 0;
}</pre>
</div>

Output:

factorial of given number

Here the factorial of number may be large so we have used unsigned long long. If the user enters a negative number then the compiler will show an error message.

factorial error message

Logic:

1st Iteration: Here at first i=1,fact=1 and n=5 i.e(i<=n).fact=fact*i i.e fact=1*1=1.now i is incremented,so i becomes 2.

2nd Iteration: Here i=2,fact=1 and n=5 i.e(i<=n).fact=fact*i i.e fact=1*2=2.now i is incremented,so i becomes 3.

3rd Iteration: Here i=3,fact=2 and n=5 i.e(i<=n).fact=fact*i i.e fact=2*3=6.now i is incremented,so i becomes 4.

4th Iteration: Here i=4,fact=6 and n=5 i.e(i<=n).fact=fact*i i.e fact=6*4=24.now i is incremented,so i becomes 5.

5th Iteration: Here i=5,fact=24 and n=5 i.e(i<=n).fact=fact*i i.e fact=24*5=120.now i is incremented,so i becomes 5.

6th Iteration: now i is incremented to 6 which is greater than n 6>5 i.e(i>n) so the program is terminated and the answer is 120.

2. Factorial Program in C Using Recursion

Recursion:

It is the method in which the function calls itself directly or indirectly. Recursion consists of two main conditions i.e base condition and the recursive call.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include<stdio.h>
int fact(int);
int main()
{
int num;
printf("Enter the number whose factorial is to be find :");
scanf("%%d" ,&num);
if(num<0)
{
printf("ERROR. Factorial is not defined for negative integers");
}
printf("Factorial of %%d is %%d", num, fact(num));
return 0;
}
int fact(int num)
{
if(num==0) //base condition
{
return 1;
}
else
{
return(num*fact(num-1)); //recursive call
}
}</pre>
</div>

Output:

factorial using recursion

In Above program int fact(int) is function prototype and in printf,fact(num) function call is made.

If the user enters 5 as the input then the call to the function fact(5) is made and 5 which is not equal to 0. So the flow goes to the else statement where return call is made to the fact(4).

This process continues till the 1 is returned and thus we get the factorial of 5 i.e 120.

3. Program to find factorial using While loop

It is the same program as a program of for loop.only difference is that we use while loop instead of while loop but the logic is the same.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include <stdio.h>
int main()
{
  int Number, i = 1; 
  long Factorial = 1;
  printf("\n Please Enter any number to Find Factorial\n");
  scanf("%%d", &Number);
  while (i <= Number)
   {
     Factorial = Factorial * i;
     i++;
   }
  printf("Factorial of %%d = %%d\n", Number, Factorial); 
  return 0;
}</pre>
</div>

Time Complexity of the above solution is O(n).

4. Program using do-while loop

In this program, we will ask user to enter the number which user want to find factorial of that number.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include<stdio.h>
#include<conio.h>
void main()
{
long n, i, fact=1;
printf("Enter any number = ");
scanf("%%ld",&n);
i=n;
if(n>=0)
{
do
{
fact=fact*i;
i--;
}
while(i>0);
printf("\nFactorial = %%ld",fact);
}
else
{
printf("fact on -ve no is not possible");
}
}</pre>
</div>

Output:

factorial using do while loop,factorial program in c

5. Program Using Pointers

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include <stdio.h>
int main()
{
  int i, Number, *P; 
  long Factorial = 1;
  printf("\n Please Enter any number to Find Factorial\n");
  scanf("%%d", & Number); 
  P = &Number;  //assigning value
  for (i = 1; i <= *P; i++)
   {
     Factorial = Factorial * i;
   }  
  printf("\nFactorial of %%d Using Normal Variable  = %%d\n", Number, Factorial);
  printf("Factorial of %%d Using Pointer Variable = %%d\n", *P, Factorial);
  return 0;
}</pre>
</div>

Output:

factorial using pointers,factorial program in c

In Pointer, we point address of Number to the address of Pointer variable. In the above program, we use P=&number where p is a pointer.

6. To find factorial using functions

This is the modular approach of the program. If we want to change in the code we need not change the whole code, instead, we will do change in function.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include <stdio.h>
long factorial(int num) //function 
{
  int i;
  long fact = 1;  
  for (i = 1; i <= num; i++)
    fact = fact * i;
 return fact; //returns to function call
} 
int main() //execution begins from main() method
{
  int num;  
  printf("Enter a number to calculate its factorial");
  scanf("%%d", &num);
  if(num<0) //if the input is a negative integer
  {
   printf("Factorial is not defined for negative numbers.");
   }
//call to factorial function passing  the input as parameter
  printf("Factorial of %%d = %%d", num, factorial(num)); 
   return 0;
}</pre>
</div>

Output:

factorial using functions,factorial program in c

7. Using Call By Reference

In this program instead of user-declared value the address of variable will pass to the function we created.

<div class="wp-block-codemirror-blocks-code-block code-block">
<pre>#include <stdio.h>
long Calculate_Factorial(int *);  
int main()
{
  int Number; 
  long Factorial = 1;
  printf("\n Please Enter any number to Find Factorial\n");
  scanf("%%d", &Number);
  Factorial = Calculate_Factorial(&Number);
  printf("Factorial of %%d = %%d\n", Number, Factorial);
  return 0;
}
long Calculate_Factorial(int *Number)
{ 
  int i; 
  long Factorial = 1;
  for (i = 1; i <= *Number; i++)
   {
     Factorial = Factorial * i;
   } 
  return Factorial;
}</pre>
</div>

Output:

factorial using call by reference,factorial program in c

Also Read: