5 Replies - 4806 Views - Last Post: 07 September 2007 - 12:38 PM Rate Topic: -----

#1 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4307
  • View blog
  • Posts: 12,085
  • 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;

// Setup your index variable
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! :)
Was This Post Helpful? 0
  • +
  • -

#3 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 zandiago  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1