C program to find factorial of a number using recursion
Learn how to write a C program to find the factorial of a number using recursion. This article includes a detailed explanation of the concept, algorithm, and complete code examples for calculating factorials using recursive functions.
The factorial of a number is a fundamental concept in mathematics, often denoted by n!
, where n
is a non-negative integer. It is defined as the product of all positive integers up to n
. Factorials have widespread applications in permutations, combinations, and various fields of mathematics and computer science. In this article, we will focus on writing a C program to find the factorial of a number using recursion. We will explain the concept of recursion, outline the algorithm, and provide a complete C program with detailed explanations.
Understanding Factorials
The factorial of a non-negative integer n
is calculated as:
n! = n x (n-1) x (n-2) x … x 2 x 1
The factorial of 0 is defined as 1, i.e., 0! = 1.
Concept of Recursion
Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. It consists of two main parts:
- Base Case: The condition under which the recursive function stops calling itself.
- Recursive Case: The part where the function calls itself with modified parameters.
Algorithm to Find Factorial Using Recursion
To calculate the factorial of a number using recursion, follow these steps:
- Base Case: If
n
is 0 or 1, return 1 as the factorial of 0 or 1 is 1. - Recursive Case: For
n > 1
, returnn * factorial(n - 1)
.
Write a C program to find factorial of a number using recursion
Below is the C program to find the factorial of a number using recursion:
#include <stdio.h>
// Recursive function to find factorial of a number
int factorial(int n) {
// Base case: Factorial of 0 or 1 is 1
if (n == 0 || n == 1)
return 1;
// Recursive case: n * factorial of (n-1)
else
return n * factorial(n - 1);
}
int main() {
int number;
// Input the number from the user
printf("Enter a number to find its factorial: ");
scanf("%d", &number);
// Check for negative numbers
if (number < 0) {
printf("Factorial of a negative number is not defined.");
} else {
// Calculate factorial using recursive function
int result = factorial(number);
// Output the result
printf("Factorial of %d is %d.", number, result);
}
return 0;
}
Output
Enter a number to find its factorial: 7
Factorial of 7 is 5040.
Explanation of the Code
- Recursive Function
factorial
:- This function takes an integer
n
as an argument and returns the factorial ofn
. - Base Case: If
n
is 0 or 1, the function returns 1. - Recursive Case: For
n > 1
, the function returnsn * factorial(n - 1)
, which recursively calls itself with the argumentn - 1
.
- This function takes an integer
- Main Function
main
:- The user is prompted to enter a number.
- The function checks if the number is negative, as factorial is not defined for negative numbers.
- The
factorial
function is called with the user-provided number, and the result is stored in the variableresult
. - The factorial result is then printed.
Example Runs
Example 1
Enter a number to find its factorial: 5
Factorial of 5 is 120.
Example 2
Enter a number to find its factorial: 0
Factorial of 0 is 1.
Example 3
Enter a number to find its factorial: -3
Factorial of a negative number is not defined.