Need help with loop program

I'm just trying to make a simple loop that adds all the integers between 1 and a maximum number. When I run the program I get a really large number, what's wrong?

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()
{
//variables
int num=1;
int max;
int sum=0;


	cout<<"Enter a positive number: ";
	cin>>max;

	while(num<=max)
	{
		sum = sum+num;
		num++;
		cout<<sum;
	}
system("pause");
}
Your code works as it should, are you trying to sum only number in between num and num + n?

lol, you forgot the newline :)
cout<<sum << endl;
Last edited on
Topic archived. No new replies allowed.