Factorial of negative number cannot be found and factorial of 0 is 1.
In this program below, user is asked to enter a positive integer. Then the factorial of that
number is computed and displayed in the screen.
Example Factorial of a given number
#include
using namespace std;
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
Output:
Enter a positive integer: 12
Factorial of 12 = 479001600
<< PREVIEW >>
<< NEXT >>
Comments
Post a Comment