【发布时间】:2022-01-21 12:48:36
【问题描述】:
在下面给定的程序中,如果我输入 n=0,则程序给出正确答案,但如果我写 n 而不是 n=0,则程序给出错误答案。如果我输入 k=10,则输出为 94,但正确答案为 55。为什么要添加额外的 39?
int main(){
// program to calculate the sum of 'n' numbers
int i=1,k,n;
printf("Enter number: ");
scanf("%d",&k);
do{
n+=i;
i++;
}while(i<=k);
printf("The sum is: %d",n);
return 0;
}
【问题讨论】:
-
提示:
n的初始值是多少?
标签: c