I need to use a while loop to print the numbers 0-100, counting up by 10s on one line. On the next line, I need to print out the sum of the numbers. So far, I have this, but evidently the sum is incorrect (My output sum=110). Any help is appreciated!
#include <iostream.h>
int main()
{
int num, sum;
sum=0;
num=0;
while (num<=100)
{
cout<<num<<"\t";
num+=10;
}
sum=sum+num;
cout<< "\n" << "The sum is: " << sum;
}
The if conditional is the problem: num<=100
At the point when num==90, it increments by 10 to 100. I assume this is the point when you want to exit the while loop. If so, the conditional should be num<100. Move the cout<<num<<"\t"; line directly after num+=10; That way it will print num when it equals 100.
Also, the sum=sum+num; line needs to go inside the while loop to be effective.