C program to find factorial of a number using recursion
ADVERTISEMENTS
In this article, you will learn how to find the factorial of a number using recursion in the c programming language.
Example
Factorial of 5 is: 120 = 5 * 4 * 3 * 2 * 1
Factorial of 6 is: 720 = 6 * 5 * 4 * 3 * 2 * 1
You should have knowledge of the following topics in c programming to understand this example:
- C Data Types
- C Recursion
- C
main()
function - C
printf()
function
Source Code
// C program to find factorial of a number using recursion
#include <stdio.h>
// @recursive function to find the factorial
long int RecursiveFacto(int fact_number) {
if (fact_number >= 1)
return fact_number * RecursiveFacto(fact_number - 1);
else
return 1;
}
// @driver function to call & find
// @factorial of a number using recursion
int main() {
int fact_number = 0;
printf("Enter a positive integer number: ");
scanf("%d", &fact_number);
// It will call the RecursiveFacto() function
printf("\nFactorial of %d is: %ld\n", fact_number, RecursiveFacto(fact_number));
return 0;
}
Output
Enter a positive integer number: 5
Factorial of 5 is: 120
Explanation
In this program, we have taken input value 5
from the user then it passes to RecursiveFacto()
recursive function.
Then It makes multiplication calculation using the recursive function to produce the output 120
.