C 如何编写一个输入为n的程序,然后计算用户输入的n个数字的总和?
到目前为止,我已经写过:C 如何编写一个输入为n的程序,然后计算用户输入的n个数字的总和?,c,C,到目前为止,我已经写过: #include<stdio.h> int main() { int n = 0, i = 0, sum = 0, a = 0; scanf("%d", &n); while (i <= n); { scanf("\n%d", &a); sum = sum + a; i++; } printf("%d", sum); } 例如,当我
#include<stdio.h>
int main()
{
int n = 0, i = 0, sum = 0, a = 0;
scanf("%d", &n);
while (i <= n);
{
scanf("\n%d", &a);
sum = sum + a;
i++;
}
printf("%d", sum);
}
例如,当我输入8时,它将不允许我添加任何其他数字
有什么问题吗?而我当我@Shabnam时,你可以使用这个代码
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}
而我
#include <stdio.h>
int main()
{
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d", &value);
sum = sum + value;
}
printf("Sum of entered integers = %d\n",sum);
return 0;
}