c语言里求1~100偶数积,我的运行结果是0,这个要怎么整呢?


因为计算过程中fact超出了int类型的范围,当fact超出范围后,高位的1会被自动截掉,最后导致fact等于0;可以将fact的类型定义为double:
#include <stdio.h>
int main()
{
int n;
double fact = 2;
for (n = 4;n <= 100;n=n+2)
fact = fact * n;
printf("fact=%1f", fact);
return 0;
}
