text

dan

·

Recursive Factorial Example

·

Plain Text

·

Total Size: 587 B

·

·

Created: 6 years ago

·

Edited: 6 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

1726 views

Are you sure you want to delete?