In this article, we will explore how to calculate the factorial of a given number using recursion in C programming. The factorial of a non-negative integer n (denoted as n!) is the product of all positive integers less than or equal to n. Recursion is a powerful programming technique where a function calls itself to solve smaller instances of the same problem, making it an ideal approach for computing factorials. We will provide a step-by-step explanation of the recursive algorithm and demonstrate its implementation in C.
Example
#include<stdio.h>
int factorialCalculation(int N){
if(N==1){
return 1;
}
else{
return N*factorialCalculation(N-1);
}
}
void main() {
int number, factorial;
printf("\nFactorial Program");
printf("\nEnter The Number: ");
scanf("%d", &number);
factorial = factorialCalculation(number);
printf("%d Factorial is: %d",number, factorial);
}
Output:
Factorial Program Enter The Number: 5 5 Factorial is: 120
Code Breakdown
Function Definition: factorialCalculation
int factorialCalculation(int N){
if(N == 1){
return 1;
}
else{
return N * factorialCalculation(N - 1);
}
}
- Purpose: This function calculates the factorial of a given number
Nusing recursion. - Parameters:
int N– the number for which the factorial is to be calculated. - Base Case:
if(N == 1), the function returns 1, since the factorial of 1 is 1. - Recursive Case:
else, the function returnsN * factorialCalculation(N - 1). This is the recursive step where the function calls itself withN - 1.
Main Function: main
void main() {
int number, factorial;
printf("\nFactorial Program");
printf("\nEnter The Number: ");
scanf("%d", &number);
factorial = factorialCalculation(number);
printf("%d Factorial is: %d",number, factorial);
}
- Variables:
int number(to store the user input) andint factorial(to store the calculated factorial). - Printing Messages:
printf("\n\nFactorial Program");prints a header for the program.printf("\n\nEnter The Number: ");prompts the user to enter a number.
- Reading Input:
scanf("%d", &number);reads the user input and stores it in the variablenumber. - Factorial Calculation:
factorial = factorialCalculation(number);calls thefactorialCalculationfunction with the user input and stores the result infactorial. - Printing Result:
printf("%d Factorial is: %d", number, factorial);prints the result showing the factorial of the entered number.
Example Walkthrough
- User Input: Suppose the user enters
5. - Function Call:
factorialCalculation(5)is called.- Recursive Calls:
5 * factorialCalculation(4)4 * factorialCalculation(3)3 * factorialCalculation(2)2 * factorialCalculation(1)
- Base Case: When
factorialCalculation(1)is called, it returns1. - Return Values: The return values are propagated back through the recursive calls:
2 * 1 = 23 * 2 = 64 * 6 = 245 * 24 = 120
- Recursive Calls:
- Result: The function returns
120forfactorialCalculation(5), which is printed as5 Factorial is: 120.
Detailed Explanation of Recursion
Recursion is a technique where a function calls itself to solve smaller instances of the same problem. In this case, the factorialCalculation function keeps calling itself with N-1 until it reaches the base case N == 1. Each recursive call contributes to the final product, effectively calculating the factorial by multiplying the sequence of numbers down to 1.
Important Points
- Base Case: Ensures the recursion terminates. Without it, the function would call itself indefinitely, leading to a stack overflow.
- Recursive Case: The function performs its primary work by calling itself with a modified argument.
- Input/Output: The
scanfandprintffunctions handle user interaction, making the program interactive.
This program is a classic example of how recursion can be used to solve problems that can be broken down into smaller subproblems.