Question about for loop.

The output I got was "Sum of numbers is 55." and I was wondering how it calculated that. If I recall correctly, the sum += num is basically sum = sum + num but I don't see how it got 55.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int sum = 0, num;
	for (num = 1; num <= 10; num++)
	{
		sum += num;
	}
	cout << "Sum of numbers is " << sum << endl;

	return 0;
}
Num=1 sum=0+1; num=2 sum=1+2; num=3 sum=3+3; num=4 sum=6+4; num=5 sum=10+5; num=6 sum=15+6; num=7 sum=21+7; num=8 sum=28+8; sum=9 sum=36+9; num=10 sum=45+10; end loop sum=55
Topic archived. No new replies allowed.