diff --git a/recursion.cpp b/recursion.cpp new file mode 100644 index 0000000..6ca9299 --- /dev/null +++ b/recursion.cpp @@ -0,0 +1,32 @@ +// Factorial of n = 1*2*3*...*n + +#include +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; + } +} + + + +Output + +Enter a non-negative number: 4 +Factorial of 4 = 24