I\'ve been trying to make a factorial function in C++, and I just found that inputs which are greater than 10 are not calculated correctly. I tried C# but I faced the same probl
This is because factorials are big numbers and the don't fit in an int
variable. It overflows. If You use unsigned long long
instead of int
, You can compute greater factorials.
If the result doesn't need to be precise, You can use double as well. In the other case, You can implement the multiplication on arrays and You can compute as big factorials as You want.
In C/C++, I recommend the GMP library
And because you asked for C family languages: Java has a BigInteger
type which is also useful.