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