Factorial calculator issues

Hello everyone.

I tried making a c++ program that asks the user for a positive number and returns the number’s factorial. I improved the program and used a while loop to repeat the question if the user enters a negative value. Surprisingly, even though the program works, it returns garbage values when I enter numbers greater than 16 and 0 for numbers greater than 20.
Please advise, your suggestions and recommendations are highly appreciated.

I suspect this is because the factorial gets extremely large beyond 20. Specifically, 21! is equal to 51,090,942,171,709,440,000 which exceeds the max value that can be stored in a 64 bit signed integer (9,223,372,036,854,775,807).

So you are getting into integer overflow at these large values and need to use a different data type to represent those huge numbers like BigInt.

I seee, thanks a lot for the guidance.

1 Like