Factorial in C family languages

后端 未结 4 1182
我寻月下人不归
我寻月下人不归 2021-01-29 06:42

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

4条回答
  •  忘掉有多难
    2021-01-29 07:04

    Variables cannot hold an infinite number of values. On a 32-bit machine, the maximum value an int can represent is 2^31-1, or 2147483647. You can use another type, such as unsigned int, long, unsigned long, long long, or the largest one, unsigned long long. Factorials are big numbers, and can easily overflow! If you want arbitrary precision, you should use a bignum library, such as GNU GMP.

提交回复
热议问题