Need help with While Loop

Why do I get 66 instead of 55? Don't know what I am doing wrong since x only goes up to 10. I wanna display it and get the sum. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

int main () {
int x, sum;
x = 0;
sum = 0; 

while ( x <= 10)
{
cout << x << endl; 
x++;

sum += x;

}

cout << sum;


}
because your cycle will produce for x and sum
x 0 1 2 3 4 5 6 7 8 9 10
sum 1 3 6 10 15 21 28 36 45 55 66

it can be a good idea to update x just before the end of the loop.

Topic archived. No new replies allowed.