In: Computer Science
Create a program that will calculate the factorial of any user entered integer. For any positive integer, the factorial is given as: n! = n·(n-1)·(n-2)·.……·3·2·1. The factorial of 0 is 1. If a number is negative, factorial does not exist and this program should display error message. Sample output dialogues should look like these:
Enter an integer: -5
ERROR!!! Tactorial of a negative number doesn't exist
Enter an integer: 10
Factorial = 3628800
#include
using namespace std;
int factorial(int n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
int main()
{
cout<<"Enter an integer: ";
int n;
cin>>n;
if(n<0)
cout<<"Error!!! Factorial of a negative number doesn't exist\n";
else
cout<<"Factorial = "<<factorial(n)<<endl;
return 0;
}