0

sum=0;

while(count<20)
cin>>num;
sum=sum+num;
count++;

I did made this one but I think it's wrong..can you help out guys?

#include <iostream>
#include <conio.h>

using namespace std;

int main()

{
int sum,num,count;
count=0;
while (count<20)
{
    cout<<"cin num"<<endl;
    cin>>num;
    sum=sum+num;
    count++;



}
cout<<"sum="<<sum<<endl;
    getch();
    return 0;
}
2
Contributors
6
Replies
19
Views
4 Years
Discussion Span
Last Post by soche123
0

Yes it is asking the user for 20 numbers to be entered one at a time. Try this and it will make it clearer what the program expects:

cout << "Enter number " << counter+1 << endl;

instead of:

cout<<"cin num"<<endl;

Edited by tinstaafl

0

Actually you can have both count and sum initialized to 0. It's generally good pracitce to initialize all variables when you declare them. It's not necessarily wrong not initializing them, but it saves you head scratching and frustration later on.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.