diff --git a/recursion.cpp b/recursion.cpp new file mode 100644 index 0000000..03f6e15 --- /dev/null +++ b/recursion.cpp @@ -0,0 +1,25 @@ +#include + +int factorial(int n) { + + if (n <= 1) { + return 1; + } + + return n * factorial(n - 1); +} + +int main() { + int n; + std::cout << "Enter a non-negative integer: "; + std::cin >> n; + + if (n < 0) { + std::cout << "Factorial is not defined for negative numbers." << std::endl; + } else { + int result = factorial(n); + std::cout << "Factorial of " << n << " is " << result << std::endl; + } + + return 0; +}