C Program to Find the Factorial of a Number using Recursion

In this article, we will write a C program to find the factorial of a number using recursion. The program takes a number from the user as input, calculates its factorial and prints the result on the output window.

Sample Example:

Enter a number: 5
Factorial of 5 is 120

Enter a number: 10
Factorial of 10 is 3628800

Enter a number: 0
Factorial of 0 is 1

Before diving into the implementation of the program, let’s first understand what is the factorial of a number.

The factorial of a number is represented by putting “!” at the end of the number. For example, 10!, 5!, etc. It is calculated as follows:

n! = n*(n-1)*(n-2)*(n-3)*......*1

The factorial for a negative number does not exist. But the factorial for zero does exist and its value is 1. We can use this as a base case for our recursive function.

To calculate the factorial of a number using recursion, we will break it down into smaller numbers recursively until we reach 0. As soon as the recursive function reaches 0, we will return 1 because the factorial of 0 is 1.

Here is the algorithm we will use:

factorial(n) = n*factorial(n-1);
factorial(0) = 1;

See implementation in the following program:

// C program to find the 
// factorial of a number

#include <stdio.h>

// Function declaration
int factorial(int);

int main() {

	int n, fact;
	
	printf("Enter a number: ");
	scanf("%d", &n);
	
    // Call the function
	fact = factorial(n);
	
	printf("Factorial of %d is %d ", n, fact);
	
    return 0;

}

// Function to calculate factorial of number
int factorial(int n){
	
	// Base case
	if(n==0)
		return 1;
		
	// If n is not 0, break it down into smaller numbers	
	return n*factorial(n-1);	
}

Output:

Enter a number: 5
Factorial of 5 is 120

Enter a number: 0
Factorial of 0 is 1

In this program, we are calling the factorial() function recursively to break down the number into smaller values.

When you are calling a function recursively, it is important to have at least one base case so that the function can return from there, otherwise, it will continue calling itself infinitely.

In our program, we took 0! as the base case. This means the function will break down the given number into smaller values until it reaches 0. As soon as it reaches 0, the function returns from there.

For example, if the user enters a positive number 5, the very first time the function will be called with the number 5 as its argument i.e. factorial(5).

Now, because the base case isn’t reached, therefore the function returns 5*factorial(4). Next, it returns 5*4*factorial(3), and so on until it reaches 0 i.e. 5*4*3*2*1*factorial(0).

As soon as the number becomes 0, the function no longer calls itself and returns from there. It then backtracks to its previous calls and finally returns the result which is stored in the fact variable.

I hope you like this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts