即使使用 long 类型,100 的阶乘函数也会在 C# 中产生 0

Factorial function for 100 results 0 in c# even though using long type

我有这个代码来确定 100 的阶乘。但是在 50 岁之后它开始给出 0 的结果。我寻找这个问题的答案,俗话说使用长变量。但是即使使用它,它仍然说答案是0。你能告诉我我在哪里出错了吗?

    static void Main(string[] args)
    {
        long c;
        int a = 100;
        c = a * (a - 1);
        for (int i=a-2; i>=1 ; i--)
        {
            c = c * i;
        }
        Console.WriteLine(c);
        Console.ReadLine();
    }`

long 类型不够长,无法存储 100 的阶乘。

改用BigInteger

BigInteger c = new BigInteger(0);
int a = 100;
c = a * (a - 1);
for (int i = a - 2; i >= 1; i--)
{
    c = c * i;
}
Console.WriteLine(c);
Console.ReadLine();

为了使用 BigInteger,您必须将对 System.Numerics 程序集的引用添加到您的项目中。 (link)