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++
cin
object - C++
cout
object
Source Code
// C++ program to find factorial of a number using recursion
#include <bits/stdc++.h>
using namespace std;
// @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;
cout << "Enter a positive integer number: ";
cin >> fact_number;
// It will call the RecursiveFacto() function
cout << "\nFactorial of " << fact_number << " is: " << RecursiveFacto(fact_number) << endl;
return 0;
}
Output
Enter a positive integer number: 7
Factorial of 7 is: 5040
Explanation
In this program, we have taken input value 7
from the user then it passes to RecursiveFacto()
recursive function.
Then It makes multiplication calculation using the recursive function to produce the output 5040
.