# Sum of integers

Page 1 of 1

## 5 Replies - 5517 Views - Last Post: 07 September 2007 - 12:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=32842&amp;s=ea237abc2b0b52a89cdfb14e7a5f415c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 zandiago

Reputation: 0
• Posts: 190
• Joined: 13-July 07

# Sum of integers

Posted 07 September 2007 - 08:59 AM

Good day:

I'm to write a C++ program that will add up all the odd numbers from 1 through 3579 and all of the even numbers from 522 through 2222. WHILE LOOPS MUST BE USED!!!

The output is to be in the format:

The sum of the odd numbers from 1-3579 is:
The sum of th eeven numbers from 522 through 2222 is:

This is what I've so far...all the help is appreciated...

```#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()

{
int num, odd, even, oddz, evenz;

for (num % 2 != 0 && num % 2 == 0)
while (odd != 3579);
{
oddz = oddz + odd;
odd += 2;
}

while  (int i = 522; i < 2222; i += 2)

cout<<"The sum of the odd numbers from 1 through 3579 is : "<<oddz<<endl;
cout<<"The sum of the even numbers from 522 through 2222 is : "<<evenz<<endl;
return 0;
}
```

Is This A Good Question/Topic? 0

## Replies To: Sum of integers

### #2 Martyr2

• Programming Theoretician

Reputation: 4820
• Posts: 13,091
• Joined: 18-April 07

## Re: Sum of integers

Posted 07 September 2007 - 09:29 AM

well you have a couple of issues here. First of all, your second while statement there is in the format of a for loop, not a while loop. Remember that a while loop tests a condition for true or false. Just like your first while statement is doing there. Secondly your for statement there is not setup correctly because it is setup more like a while loop and the value of "num" hasn't been set so you can't use it in those expressions.

If you setup everything up correctly you will only need three variables instead of four. One variable to act as your counter and the other two to store your odd and even totals. Here is the general idea...

```
// Set your odd and even variables
int odd = 0;
int even = 0;

int i = 1;

// Loop through the entire range
while (i <= 3579) {

if ((i % 2)  == 1) { // Add number to your odd variable }

if ((i >=  522) && (i <= 2222)) {
// Check if it is even and add to your even variable
}

i++;

}

```

Now I am not sure if they expected more than one while loop, but one while loop is all that it would take since the even numbers range is within the odd numbers range. Now if at any time that part of the even numbers range lies OUTSIDE the odd numbers range, you will just need to break it into two loops and sum them separately.

Hope this helps!

### #3 zandiago

Reputation: 0
• Posts: 190
• Joined: 13-July 07

## Re: Sum of integers

Posted 07 September 2007 - 09:49 AM

Ok sounds good....imma redo my code and post along with a few other questions for you...assistance is really appreciated...thx much

### #4 zandiago

Reputation: 0
• Posts: 190
• Joined: 13-July 07

## Re: Sum of integers

Posted 07 September 2007 - 10:09 AM

Ok, thanx..i got it to work...i was just wondering (apart from the program itself) if there would be anyway to verify if what the program did is correct mathematically?

### #5 Bench

• D.I.C Lover

Reputation: 859
• Posts: 2,343
• Joined: 20-August 07

## Re: Sum of integers

Posted 07 September 2007 - 12:31 PM

Mathematically, the way to get the sum of all numbers in a range X..Y
Where 'N' is the number of numbers from X..Y inclusively

N * ( X+Y ) / 2

eg, all numbers 1..10 (1+10) * 10 / 2 = 55
all odd numbers from 1..101 (1+101) * 51 / 2 = 2601
all even numbers from 50..100 (50+100) * 26 / 2 = 1950

This post has been edited by Bench: 07 September 2007 - 12:33 PM

### #6 zandiago

Reputation: 0
• Posts: 190
• Joined: 13-July 07

## Re: Sum of integers

Posted 07 September 2007 - 12:38 PM

oh ok...i see it now.....thx alot Bench and have a good weekend.