2012-02-21 4 views
-5
#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(); 
} 

Программа предоставляет разные выходы в виде суммы. Я не могу найти ошибку. Помощь будет действительно оценена.Программа для добавления n целых чисел, не работающих в C

+10

Приведите пример из вы получаете, и объясните, почему вы считаете, что это неправильно. –

+4

Приведите пример * ввода *, который вы используете. – ydroneaud

+0

Кроме того, почему вы вызываете 'getch()' в конце и не присваиваете его чему-либо? –

ответ

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