# why do i get 5050?

• 04-05-2006
findme
why do i get 5050?
Code:

```#include <iostream> #include <stdlib.h> #include <string> using namespace std; int main(int argc, char *argv[]) { int x = 0; int i; for (i = 1; i <= 100; i++){     x += i; } cout << x << endl;      system("PAUSE");          return 0; }```

how i get 5050 out of this? the tutorial i got i from doesn't tell me anything about it and im very confused.
• 04-05-2006
rwmarsh
Quote:

Code:

```for (i = 1; i <= 100; i++){     x += i; }```

You are adding i to x. i is increased by one each round though the loop so that by the 100th time through the loop you add 100 to x
• 04-05-2006
JaWiB
Code:

```1  + 2  + 3  + 4  + ... + 100 100 + 99  + 98  + 97  + ... + 1 101 + 101 + 101 + 101 + ... + 101 101(100) = 10,100 10,100/2 = 5050```
And thus the formula: n(n+1)/2
• 04-05-2006
findme
o thanks man

u guys must know a lot about loops.
• 04-05-2006
SlyMaelstrom
Quote:

Originally Posted by findme
o thanks man

u guys must know a lot about loops.

Yeah, I sometimes wonder how they do it.