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;
}

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 3 Years Ago by tinstaafl

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.