Hi all,

I have a assignment that says, write a program that will add up all of the odd numbers from 1 through 3579 and alll of the even numbers from 522 to 2222.

Use While loops.

Output should look like this

The sum of the odd numbers from 1 through 3579 is XXXXXXXXXXX
The sum of the even numbers from 522 through 2222 is XXXXXXXXX.

Where X is the answer.

I started off with the program trying to just ititailize the variables and let the program go but it wasnt doing anything so I made the user input a value in the beginning but I think he just wants it to run on its own with no input from the user.

This is what I have so far.

int odd = 1;
int even = 522;
int totalodd = 0;
int totaleven = 0;
int number;

cout << "Enter an intial odd # between 1 - 3579 or an even # between 522 - 2222)" << endl;
cin >> number;
if (number % 2 != 0){

   while (odd != 3579){
   totalodd = totalodd + odd;
   odd + 2;}
}

else if (number % 2 == 0){

   while (even !=2222){
         totaleven = totaleven + even;
         even + 2;
}}

cout << "The sum of the odd numbers from 1 through 3579 is " << totalodd << endl;
cout << "The sum of the even numbers from 522 through 2222 is " << totaleven << endl;

as of now it does compile but doesnt do anything after the individual enters a number.

Thanks Alot for the help

Edited 3 Years Ago by pyTony: fixed formatting

It looks like your logic is reversed a bit. You want to sum both the odds and evens, but in your current logic, it's either one or the other depending on the initial number.

Since the range of odd numbers is wider than even numbers, you can include the test and sum of even numbers in your loop for the odd numbers. Just do the sum for even numbers conditionally:

for ( int i = 0; i < 3579; i++ ) {
  if ( i % 2 != 0 )
    totalodd += i;

  if ( i >= 522 && i < 2222 && i % 2 == 0 )
    totaleven += i;
}

Hi all,

I have a assignment that says, write a program that will add up all of the odd numbers from 1 through 3579 and alll of the even numbers from 522 to 2222.

Use While loops.

Output should look like this

The sum of the odd numbers from 1 through 3579 is XXXXXXXXXXX
The sum of the even numbers from 522 through 2222 is XXXXXXXXX.

Where X is the answer.

I started off with the program trying to just ititailize the variables and let the program go but it wasnt doing anything so I made the user input a value in the beginning but I think he just wants it to run on its own with no input from the user.

This is what I have so far.

int odd = 1;
int even = 522;
int totalodd = 0;
int totaleven = 0;
int number;
 
cout << "Enter an intial odd # between 1 - 3579 or an even # between 522 - 2222)" << endl;
cin >> number;
if (number % 2 != 0){
 
while (odd != 3579){
totalodd = totalodd + odd;
odd + 2;}
}
 
else if (number % 2 == 0){
 
while (even !=2222){
totaleven = totaleven + even;
even + 2;
}}
 
cout << "The sum of the odd numbers from 1 through 3579 is " << totalodd << endl;
cout << "The sum of the even numbers from 522 through 2222 is " << totaleven << endl;

as of now it does compile but doesnt do anything after the individual enters a number.

Thanks Alot for the help

ok, first, please use code tags when posting code...
second, lines 13 and 20 do absolutely nothing - I think you meant to say something like "odd+=2;" or "even+=2;"...and you're right, there's no need for the initial user input at the beginning...
hope that helps...

so far I made a few changes thanks for the assistance so far I got past that I did get it so show up the first number as I need it to but I cant get the second number to show up.
but then I tried making some changes to it but now cant get back to where I was.

Hope someone can give me some assistance, I am new to C++ and I'm racking my brain.

I appreciate all the help

for ( int odd = 1; odd < 3579; odd++ )

    while (odd != 3579; number % != 0){
          totalodd = totalodd + odd;
          odd += 2;}
    }


    while (even !=2222; number % == 0){
          totaleven = totaleven + even;
          even += 2;
    }}

Edited 3 Years Ago by pyTony: fixed formatting

Actually I had to open disregard last code this is the one I have so far I get the responce I am suppose to get but only for top line.

if (number % 2 != 0){

while (odd != 3579){
totalodd = totalodd + odd;
odd += 2;}
}

else if (number % 2 == 0){

while (even !=2222){
totaleven = totaleven + even;
even += 2;
}}

>I am new to C++
Then I'll encourage you not to use that horrid style of formatting. Ideally you should put braces on a separate line. Look for code posted by members here with high post counts. Chances are good that they use a highly readable style.

>but then I tried making some changes to it
In other words, you took the example I gave you (which worked perfectly according to your requirements) and butchered it.

>but now cant get back to where I was
This is where versioning helps. Unfortunately, I can't help you get back to where you were because I don't know where you were unless where you were was the code I posted, in which case you can just cut and paste it again.

>disregard last code this is the one I have so far
Grrr. You're right back where you started. The if statement guarantees that you calculate evens, or odds, but never ever ever both. I explained this to you in post #2.

I am sorry for all the confusing beginner problems, I seem to be upsetting you, I appoligize, Im trying to do my best to understand something that to me is harder than a foreign language.

I wanted to ask you about the "braces on a separate line part, I'm not sure what you mean by that. Also I did not take what you had and just substitute what I have into it. I have been lagitamently tyring to work on this for awhile.

I used what you had wrote down and tried to see how to incorporate that into what I have and understand not that it works but why it works. I tried replacing if with for on the program now but I still get a error that it wont compile I'm going to post it up but don't get mad b/c it looks bad or not in the format your used to I'm sorry again I don't want to sound like sum scumbag that's trying to have you do my homework for me, I'm just trying to understand this stuff that seems to be extremely hard for me.

So far:

for (number % 2 != 0) && (number % 2 == 0){ \\not sure if this should look like this but I think this may show for odds and for evens?

{odd = (number % 2 != 0); \\also not sure if this is what I want or not.
while (odd != 3579)
totalodd = totalodd + odd;
odd += 2;
}

{even = (number % 2 == 0);
while (even !=2222)
totaleven = totaleven + even;
even += 2;
}
}

Sorry for the constant questions I'm just trying to learn

>I seem to be upsetting you
I doubt you could manage to upset me.

>I wanted to ask you about the "braces on a separate line part, I'm not sure what you mean by that.
I mean that anywhere you use { or }, they should be alone on the line and everything between them should be indented. Like this:

void function()
{
  // statement

  if ( something )
  {
    // statement
    // statement
    // statement
  }

  // statement

  while ( something )
  {
    // statement
  }

  // statement
}

This guarantees that it's easy to see where in the nesting each statement is, and you save yourself a lot of trouble trying to match braces because a missing brace is painfully obvious.

>I'm just trying to understand this stuff that seems to be extremely hard for me.
Okay, let's keep it as simple as possible. Treat the odd and even calculations as completely separate. If nothing is related, the relationships won't confuse you. Then it boils down to two separate loops:

int oddsum = 0;

for ( int i = 1; i < 3579; [I]next number[/I] ) {
  // add the odd numbers to oddsum
}

int evensum = 0;

for ( int i = 522; i < 2222; [I]next number[/I] ) {
  // add the even numbers to evensum
}

Now, we can do this two ways. We can look at every number and pick out the evens or the odds, but that's slightly more complicated because you have to determine if the number is even or odd. An easier way is to just add 2 in the part that says next number. That way you skip over the numbers you don't want:

int oddsum = 0;

for ( int i = 1; i < 3579; i += 2 ) {
  // add the odd numbers to oddsum
}

int evensum = 0;

for ( int i = 522; i < 2222; i += 2 ) {
  // add the even numbers to evensum
}

Adding to the sum is easy. Just do sum = sum + i , or the shorter form of sum += i .

That's all there is to it. Look through the range by adding 2, and add the current number in the range to the sum. Do this twice: once for odd numbers and once for even numbers.

Narue,

I want to thank you very much for all of your help, I have read over and used what you taught me to incorporate it into the program, and then re-read over it to understand what is going on, and I am seeming to understand almost all of it, I wanted to Thank You very much for all of your help. I am going to take a break for now from my Homework but will continue later today.

Have a Great day and thanks for the help!!!
Radskate360

I guess that his professor was real adamant in him using while loops for the program instead of the good 'ol for loops...

i was just wondering (apart from the program itself) if there would be anyway to verify if what the program did is correct mathematically?

This question has already been answered. Start a new discussion instead.