From bd4d004004d911177a6193456a8ff9f9f040f0b9 Mon Sep 17 00:00:00 2001 From: priyani25 <138353487+priyani25@users.noreply.github.com> Date: Tue, 31 Oct 2023 22:55:58 +0530 Subject: [PATCH] Create recursion.cpp --- recursion.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 recursion.cpp diff --git a/recursion.cpp b/recursion.cpp new file mode 100644 index 0000000..e881e73 --- /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; +}