Sum of the numbers between two integers

This is a discussion on Sum of the numbers between two integers within the C++ Programming forums, part of the General Programming Boards category; I need help here. I need a program that will calculate the sum of the numbers between two integers that ...

1. Sum of the numbers between two integers

I need help here. I need a program that will calculate the sum of the numbers between two integers that are assigned a value using cin. For example, say the two numbers were 25 and 47. Using a for loop, how do you calculate
"25+26+27+28+29+....+47"

2. You can change the beginning and ending values for the for loop, they don't always have to start at 0.

I'd say make a program that takes in two numbers from the user and make sure it compiles and works. Then add the code to just output the numbers (without adding them) in the loop. Make sure that compiles and works. Then instead of outputting them, add them together. Make sure it compiles and works. Then you are done. If you get stuck along the way, post the code you've done so far and ask a specific question about what you are stuck with.

3. So far i have this

Code:
```#include<iostream>
using namespace std;

int main()
{

int start, end;

cout<<"Enter one #: ";
cin>>start;
cout<<"The second #: ";
cin>>end;

for (int x=start; x<=end; x++)
{

This is the part I need. The math of the program.

}

cout<<"The sum of the integers btwn these 2 numbers is "<<x;

return 0;
}```

I need the statements in the for loop.

4. Code:
`removed`

5. That results in far too little, neandrake.

Sridar, you need an additional variable to hold the sum.

Code:
```#include<iostream>
using namespace std;

int main()
{

int start, end;

cout<<"Enter one #: ";
cin>>start;
cout<<"The second #: ";
cin>>end;

int sum = 0;
for (int x=start; x<=end; x++)
{

Can you guess now?

}

cout<<"The sum of the integers btwn these 2 numbers is "<<sum;

return 0;
}```

6. I still dont get it. Just Tell me what goes in the for loop.

7. sum += x;

8. >> That results in far too little, neandrake.

It didn't feel right as I typed it, but it looks correct to me. What's the problem?

shyte! Man, I must be high ;-)
Yea, pay no attention to my code.

You should create a variable outside of the for loop, set to zero. Inside the for loop, just add i to the new variable and it adds them up.
[/edit]

9. *Sridar bangs his head on the desk*

!bang,bang,bang!

Thank you so much Corned Bee

10. Originally Posted by Sridar
*Sridar bangs his head on the desk*

!bang,bang,bang!

Thank you so much Corned Bee

//me follows

11. I know this is irrelevant because this is a homework about for-loops, but I still have to point out:
The sum of every integer between and including a and b is
(a+b)(a-b+1)/2

12. wow, who agrees I should get negative reputation for a small mistake in the code? (removed)

13. Originally Posted by Sang-drax
I know this is irrelevant because this is a homework about for-loops, but I still have to point out:
The sum of every integer between and including a and b is
(a+b)(a-b+1)/2
Well (a-b+1) is the number of numbers between a and b assuming a is the larger. Multiply it by the average of both numbers. Yea, it seems like it should give you the right answer. However, you might get wrong answers since working with integers.

14. No. As the sum of all these numbers can only be an integer, the formula logically can only yield an integer, too, even if it were to be executed in floating point calculations.
Or, in a less intuitive but more correct way:
In order for the /2 to yield an integer, the left side must be even.
(a+b)(a-b+1) -> even
An expression of the form x*y is even if either or both of the terms are even, or reverse, it's uneven exactly when both terms are uneven.
x is (a+b)
y is (a-b+1)
Since the evenness of (a+b) and (a-b) is the same, the +1 makes sure that, if (a+b) is even, (a-b+1) is uneven and vice versa. This means that x and y always have different evenness, that is, the above condition (both uneven) can never be fulfilled. (a+b)(a-b+1) is always even and the divison by two thus always yields an integer.