2*4*6*8*10*...*100

我输出的结果是0,有人知道哪里出问题了吗?请指教
#include "stdio.h"
int main()
{
int n=1,chengji=1,a=2;
while(n<=50)
{
n=n+1;
chengji=chengji*a;
a=a+2;
}
printf("计算结果是%d\n",chengji);
return 0;
}

int型太短,数太大,溢出了
改成long long int试试
实在不行就只能自己实现大数计算了

超范围了吧

数太大了,int类型范围-2147483648 ~ 2147483647 超出就会出错

首先,你可以试试1 * 2 * 3 * 4.。。* 50,试试看是多少?
另外,50 个2 就是2^50次方,这个相乘之后。。。。。

#include "stdio.h"
int main()
{
long long result = 1;
int n = 1;
while(++n<=50)
{
    result *= n;
    printf("temp result: %lld, n: %d\n", result, n);
}
printf("the result = %lld\n", result);
return 0;
}

int型太短,数太大,溢出了
改成long long int试试
实在不行就只能自己实现大数计算了

img

img


看最后一次有效数据