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
12 Answers
+ 9
You can use long long int instead of using long int.
+ 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.
+ 2
long is often of size 4, like int. Use unsigned int/long or long long.
+ 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
+ 1
If you want to get higher, maybe you need tricks. :)
https://code.sololearn.com/cV3o3NWxVZ11/?ref=app
+ 1
~ swim ~, isn't that for normal double? Or does it work for both?
0
Ok so used long int .
0
~ swim ~ hmm ok and what is format specifier for long double