While Loop - Counting by 10s

Hello! -

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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;
   
   } 
Last edited on
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.
Last edited on
@billywilliam, Thanks for the help!
Topic archived. No new replies allowed.