0

I have a kinda error in my factorial code

https://code.sololearn.com/cwoskeEq842e/?ref=app You can click the link to check my code. This program is giving correct factorial values upto integer 12 it's not working when we enter a variable greater than 12.can any genius explain this

6th May 2019, 10:37 AM
ravi shanker
12 Answers
+ 9
You can use long long int instead of using long int.
8th May 2019, 3:19 PM
Yamin Mansuri
Yamin Mansuri - avatar
+ 3
type int can only hold numbers up to something above 2.1 billions, then it 'overflows'. See for example here: https://docs.microsoft.com/en-us/cpp/c-language/cpp-integer-limits?view=vs-2019 You can choose an unsigned int, then you can go higher; you can also choose another type, typical choice would be unsigned long long. But also in that case, you'll be limited to about 20!, then you'll again have to deal with overflow.
6th May 2019, 10:48 AM
HonFu
HonFu - avatar
+ 2
long is often of size 4, like int. Use unsigned int/long or long long.
6th May 2019, 10:52 AM
HonFu
HonFu - avatar
+ 1
You can look at the code. I changed the type of "a" to long long and now it works. Hope it helps you. https://code.sololearn.com/cKYMM1k0lWcu/?ref=app
6th May 2019, 10:52 AM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
If you want to get higher, maybe you need tricks. :) https://code.sololearn.com/cV3o3NWxVZ11/?ref=app
6th May 2019, 12:14 PM
HonFu
HonFu - avatar
+ 1
~ swim ~, isn't that for normal double? Or does it work for both?
6th May 2019, 12:27 PM
HonFu
HonFu - avatar
0
Ok so used long int .
6th May 2019, 10:50 AM
ravi shanker
0
~ swim ~ hmm ok and what is format specifier for long double
6th May 2019, 12:19 PM
ravi shanker