2012-02-21 3 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(); 
} 

プログラムは合計として異なる出力を出しています。私は誤解を見つけることができません。ヘルプは本当に感謝します。Cで動作しないn個の整数を加算するプログラム

+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 
関連する問題