From 3dadda356cff40115ecba7eb678cdfc457f8720b Mon Sep 17 00:00:00 2001 From: palakpreet24 Date: Tue, 31 Oct 2023 23:29:37 +0530 Subject: [PATCH] factorial added --- factorial.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 factorial.cpp diff --git a/factorial.cpp b/factorial.cpp new file mode 100644 index 0000000..6cc67c2 --- /dev/null +++ b/factorial.cpp @@ -0,0 +1,23 @@ +#include +using namespace std; + +int factorial(int n); + +int main() { + + int n; + + cout << "Enter a positive integer: "; + cin >> n; + + cout << "Factorial of " << n << " = " << factorial(n); + + return 0; +} + +int factorial(int n) { + if(n > 1) + return n * factorial(n - 1); + else + return 1; +} \ No newline at end of file