RECURSIVE FUNCTIONS
C++ RECUSIVE FUNCTIONS
A function that calls itself is known as a recursive function. And, this technique is known as recursion.
Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main() {
int n, result;
cout << "Enter a non-negative number: ";
cin >> n;
result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}
int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}
Comments
Post a Comment