Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions C++/Factorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"<<endl;
cin>>n;
fact=factorial(n);
cout<<"Factorial of the number you entered is "<<fact<<endl;
return 0;
}