as homework I need a program that reads a nonnegative integer and computes and prints its factorial. So far I wrote the code but if I try to input 50! the result is 0. It works with smaller numbers. Any help would be much appreciated.
#include <iostream>
using namespace std;
int main()
{
int counter = 1;
int number;
cout << "Please enter a number: ";
cin >> number;
int factorial = number;
while (counter != number)
{
factorial = factorial * (number - counter);
counter++;
}
cout << "The factorial of " << number << "! is: " << factorial << endl;
return 0;
}
50!
is 30414093201713378043612608166064768844377641568960512000000000000
, far too large for an int. Due to integer overflow, the result is 0.