text
dan
·
Recursive Factorial Example
·
Plain Text
·
Total Size: 587 B
·
·
Created: 5 years ago
·
Edited: 5 years ago
// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int n;
cout<<"Enter a number to find factorial: ";
cin >> n;
cout << "Factorial of " << n <<" = " << factorial(n);
return 0;
}
int factorial(int n)
{
if (n > 1)
{
return n*factorial(n-1);
}
else
{
return 1;
}
}
An example of a recursive function to calculate the factorial of a number.
The output of the script would be:
Enter a number to find factorial: 4
Factorial of 4 = 24
2 bits
•
1630 views
Are you sure you want to delete?