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

Recommended Answers

All 6 Replies

Why do you think it's wrong? Don't you get the sum of the numbers? Does it produce an error?

sir tinstaafi, it keeps cin-ing nums -_-

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;

thanks but shouldn't the 10th line be

sum=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.

Thanks a lot sir!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.