我刚刚了解了整数溢出,但需要帮助修复它
int factorial()
{
int answer = 1;
int n;
int factNum;
printf("Please enter a postive integer:\n");
// user input is stored in factNum
scanf("%i", &factNum);
for(n =1; n <= factNum;n++)
{
answer = answer * n;
}
return answer;
}
我刚刚了解了整数溢出,但是我需要帮助修复它int factorial(){int answer = 1; int n; int factNum; printf(“请输入一个正整数:\ n”); //用户输入为...
int
最多只能存储2^31 - 1
个数字(尽管从技术上讲,这是实现定义的)。如果您希望数字最大为2^64
(大致为21!
),请将int answer = 1;
更改为unsigned long long answer = 1;
。