From 97649549d84771cdabe3c33eba8ccba2c597def4 Mon Sep 17 00:00:00 2001 From: jkunal14 <43882559+jkunal14@users.noreply.github.com> Date: Sat, 6 Oct 2018 22:22:05 +0530 Subject: [PATCH] Update Factorial.cpp --- C++/Factorial.cpp | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/C++/Factorial.cpp b/C++/Factorial.cpp index 19c330bf6..617f10435 100644 --- a/C++/Factorial.cpp +++ b/C++/Factorial.cpp @@ -3,18 +3,27 @@ using namespace std; int factorial(int n) { - if(n > 1) - return n * factorial(n - 1); + if(n<0) + { + return(-1); //Corresponds to wrong value + } + if(n==0) + { + return(1); //Terminating condition + } else - return 1; - + { + return(n*factorial(n-1)); //recursive call + } } int main() { - int n; - cout << "Enter a positive integer: "; - cin >> n; - cout << "Factorial of " << n << " = " << factorial(n); + int factorial(int); + int fact,n; + cout<<"Enter the number of which u wish to get factorial"<>n; + fact=factorial(n); + cout<<"Factorial of the number you entered is "<