Factorial Using Recursion in C

Here, we will find factorial using recursion in C programming language. Prerequisites:- Recursion in C Programming Language

Program description:- Write a C program to find factorial of a number using recursion techniques.

Factorial of a number n is given by 1*2*….*(n-1)*n and it’s denoted by n!
Example Factorial of 4= 4! = 4*3*2*1 or 1*2*3*4

Generally, Factorial of a number can be found using the for loop and while loop. But it can also find using Recursion. Here the problem of finding n! divide them into smaller problem n  *  (n-1)!

We know that
4! = 4*3*2*1
We can write it as,
4! = 4 * 3!
Similarly, we can write
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1

So, in general, we can say that factorial of a positive integer n is the product of n and factorial of (n-1).
n! = n * (n-1)!;

Now, the problem of finding out factorial of (n-1) is similar to that of finding out factorial of n, but it is smaller in size. So, we have defined the solution of the factorial problem in terms of itself. We know that the factorial of 0 is 1 and similarly factorial of 1 is 1. This can act as the terminating condition or the base case.

The base case for finding factorial
factorial(0) = 1; Or,
factorial(1) = 1;
General case for finding factorial
factorial(n) = n * factorial(n-1);

Program for factorial using recursion in C

#include<stdio.h>

// recursive function to find factorial of a number
int factorial(int n) 
{
     if(n!=0)
         return n*factorial(n-1); // general case
     else
         return 1; // base case
}

int main()
{
     int num, result;
     printf("Enter a positive number: ");
     scanf("%d",&num);
     result= factorial(num); //function call
     printf("Result = %d\n",result);
     return 0;
}

Output for the different test-cases:-

Enter a positive number: 5
Result = 120

Enter a positive number: 7
Result = 5040

Explanation:-
In this C program, one recursive function factorial is developed which returns int value and takes an int as an argument and store it into the parameter. Using the base case and general case discussed before the program, the if-else statement is written. This recursive function will return 1 when the number is 1, else it will again call the recursive function. The recursive function is called from the main method.

For finding the factorial of number 5, a recursive function is called with argument 5. The number is not equal to 0, so if the condition becomes true and recursive function called again with number 4. The same thing happens until the number becomes 0. Now, num!=0 becomes true then else part will be executed and it will return 1. Finally, the recursive function will return 5*4*3*2*1*1 to the main function.

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!

Leave a Comment

Your email address will not be published. Required fields are marked *