程序添加n个不在C中工作的整数

时间:2012-02-21 15:45:16

标签: c

#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();
}

该计划提供不同的输出作为总和。我无法找出谬误。帮助将非常感激。

1 个答案:

答案 0 :(得分:1)

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

以下代码正常工作(一旦你摆脱了那个非标准的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