gpt4 book ai didi

c - 将 n 个整数相加的程序在 C 中不起作用

转载 作者:行者123 更新时间:2023-11-30 21:38:20 25 4
gpt4 key购买 nike

#include <stdio.h>
int main(void)
{
int n,c,value,sum=0;
printf ("Enter the no of integers u want to add:");
scanf ("%d",&n);
printf ("\nEnter %d integers:",n);
for (c=1;c<=n;c++)
{
scanf ("%d",&value);
sum=sum+value;
}
printf ("\nSum of the integers:%d",sum);
getch();
}

程序给出不同的输出作为总和。我无法找出其中的谬误。非常感谢您的帮助。

最佳答案

您的设置还存在其他问题,此处未显示。

以下代码可以正常工作(一旦您摆脱了非标准的 getch() 令人厌恶的行为并从 main 返回一个值):

#include <stdio.h>
int main(void) {
int n,c,value,sum=0;
printf ("Enter the no of integers u want to add:");
scanf ("%d",&n);
printf ("Enter %d integers:",n);
for (c=1;c<=n;c++) {
scanf ("%d",&value);
sum=sum+value;
}
printf ("Sum of the integers:%d\n",sum);
return 0;
}

文字记录:

pax> ./qq
Enter the no of integers u want to add:3
Enter 3 integers:1 2 3
Sum of the integers:6

pax> ./qq
Enter the no of integers u want to add:5
Enter 5 integers:10
20
30
40
50
Sum of the integers:150

关于c - 将 n 个整数相加的程序在 C 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9380480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com