# Factorial calculator issues

**URL:** <https://forum.codewithmosh.com/t/factorial-calculator-issues/16865>\
**Category:** C++\
**Created:** [December 4, 2022, 6:24pm UTC](https://forum.codewithmosh.com/t/factorial-calculator-issues/16865 "2022-12-04T18:24:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SALIFOULTN](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@SALIFOULTN](https://forum.codewithmosh.com/u/SALIFOULTN)\
**Post date:** [December 4, 2022, 6:24pm UTC](https://forum.codewithmosh.com/t/factorial-calculator-issues/16865/1 "2022-12-04T18:24:59Z")

</div>

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.

 ![2022-12-04](https://us1.discourse-cdn.com/flex020/uploads/codewithmosh/original/2X/1/1436e0cb391e0de918781f047ca2a53ad39c420f.png)

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [December 4, 2022, 6:33pm UTC](https://forum.codewithmosh.com/t/factorial-calculator-issues/16865/2 "2022-12-04T18:33:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SALIFOULTN](https://avatars.discourse-cdn.com/v4/letter/s/13edae/32.png) [@SALIFOULTN](https://forum.codewithmosh.com/u/SALIFOULTN)\
**Post date:** [December 6, 2022, 12:41am UTC](https://forum.codewithmosh.com/t/factorial-calculator-issues/16865/3 "2022-12-06T00:41:45Z")

</div>

I seee, thanks a lot for the guidance.
