Introduction
The factorial of a non-negative integer ( n ) is the product of all positive integers less than or equal to ( n ). Factorials are commonly used in permutations, combinations, and other mathematical calculations. This guide will show you how to write a C program to find the factorial of a number using recursion.
Factorial Definition:
- ( n! = n \times (n-1) \times (n-2) \times \dots \times 1 )
- Special case: ( 0! = 1 )
Example:
- Input: ( n = 5 )
- Output: ( 5! = 120 ) (since ( 5 \times 4 \times 3 \times 2 \times 1 = 120 ))
Problem Statement
Create a C program that:
- Takes a non-negative integer input from the user.
- Uses a recursive function to calculate the factorial of the number.
- Displays the factorial.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>for standard input-output functions. - Write the Recursive Function: Define a recursive function that calculates the factorial of a number.
- Write the Main Function: Define the
mainfunction to take user input and call the recursive function. - Input the Number: Use
scanfto take input from the user for the number. - Call the Recursive Function: Pass the input number to the recursive function to calculate the factorial.
- Display the Result: Use
printfto display the factorial of the number.
C Program to Find the Factorial of a Number Using Recursion
#include <stdio.h>
// Step 2: Define the recursive function to calculate factorial
int factorial(int n) {
if (n == 0) {
return 1; // Base case: 0! = 1
} else {
return n * factorial(n - 1); // Recursive case: n! = n * (n-1)!
}
}
int main() {
// Step 3: Declare variables to hold the number and factorial result
int num;
int result;
// Step 4: Prompt the user to enter a non-negative integer
printf("Enter a non-negative integer: ");
scanf("%d", &num);
// Check for negative input
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Step 5: Call the recursive function to calculate the factorial
result = factorial(num);
// Step 6: Display the result
printf("Factorial of %d is %d\n", num, result);
}
return 0; // Return 0 to indicate successful execution
}
Explanation
Step 2: Define the Recursive Function
- The
factorialfunction takes an integernas input. - Base Case: If
nis 0, the function returns 1 (since ( 0! = 1 )). - Recursive Case: If
nis greater than 0, the function returnsn * factorial(n - 1), which recursively calls itself with the valuen - 1until it reaches the base case.
Step 3: Declare Variables
- The variable
numstores the input number. - The variable
resultstores the factorial calculated by the recursive function.
Step 4: Input the Number
- The program prompts the user to enter a non-negative integer using
scanf.
Step 5: Call the Recursive Function
- The program calls the
factorialfunction withnumas the argument and stores the result inresult.
Step 6: Display the Result
- The program uses
printfto display the factorial of the input number.
Return 0
- The
return 0;statement indicates that the program executed successfully.
Output Example
Example 1:
Enter a non-negative integer: 5
Factorial of 5 is 120
Example 2:
Enter a non-negative integer: 0
Factorial of 0 is 1
Example 3:
Enter a non-negative integer: -3
Factorial is not defined for negative numbers.
Conclusion
This C program demonstrates how to calculate the factorial of a number using a recursive function. It covers basic concepts such as recursion, base cases, and user input, making it a useful example for beginners learning C programming.