From fc3d7a052663b1f4bb88dc73d6dc486f48de099d Mon Sep 17 00:00:00 2001 From: sakshi292 <114948270+sakshi292@users.noreply.github.com> Date: Sat, 8 Oct 2022 14:44:43 +0530 Subject: [PATCH] Create C++3040cpp --- C++3040cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 C++3040cpp diff --git a/C++3040cpp b/C++3040cpp new file mode 100644 index 0000000..17a4da1 --- /dev/null +++ b/C++3040cpp @@ -0,0 +1,25 @@ +// 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; + } +}