Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,669 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,169 people online right now. Registration is fast and FREE... Join Now!




Write a program that uses loops to output sum of all even numbers betw

2 Pages V  1 2 >  
Closed TopicStart new topic

Write a program that uses loops to output sum of all even numbers betw

monkjessica
post 18 Jul, 2007 - 02:57 PM
Post #1


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 10


My Contributions


I need a while loop to do the following:
A:Get the user to input 2 integer values;
B:Output all of the odd #'s between the odds;
C:Output the sum of all the even #'s between them;
D:output #'s and their squares between 1 and 10;
E:Output the sum of the squares of the odd numbers between them.
F: Output all uppercase letters.

I think I have A working correctly but I am hung up on B. Any help is greatly appreciated!
Here is what I have so far.

CODE


#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{
    int firstNum;
    int secondNum;
    int n;
    int sum;
    
    cout << "Please enter two numbers." << endl;
    cout << "(First Number must be less than second number.)" << endl;
    cin >> firstNum >> secondNum;

    
    for(n=firstNum;n<=secondNum;n++)
{
if(n%2==0)
{
   //even number
}
else
  // odd number
{
cout << "The odd numbers are:  " << n << endl;

}
}
for(int n=firstNum;n<=secondNum;n++)

  if(n%2==0)
     sum += n;
  else
     //odd
{
     cout<< "The sum of all even numbers between two numbers:  " << sum << endl;
}


system ("PAUSE");
return 0;

}



This post has been edited by monkjessica: 18 Jul, 2007 - 02:59 PM
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 18 Jul, 2007 - 04:30 PM
Post #2


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


Hi, monkjessica and welcome to dream in code. I see that you don't use a while loop, you are using a for loop.
User is offlineProfile CardPM

Go to the top of the page

monkjessica
post 18 Jul, 2007 - 05:12 PM
Post #3


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 10


My Contributions


QUOTE(PennyBoki @ 18 Jul, 2007 - 05:30 PM) *

Hi, monkjessica and welcome to dream in code. I see that you don't use a while loop, you are using a for loop.


At this point I will take any thing that will make this code run. Any suggestions?
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 18 Jul, 2007 - 05:20 PM
Post #4


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


I'm a little confused about this
QUOTE
B:Output all of the odd #'s between the odds;

so let say user enters 3 an 12.
If I follow the logic of the above request to display the odd between the odds
then my output would be:
5,7,9
is this what question B is about?

This post has been edited by PennyBoki: 18 Jul, 2007 - 05:21 PM
User is offlineProfile CardPM

Go to the top of the page

monkjessica
post 18 Jul, 2007 - 05:30 PM
Post #5


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 10


My Contributions


QUOTE(PennyBoki @ 18 Jul, 2007 - 06:20 PM) *

I'm a little confused about this
QUOTE
B:Output all of the odd #'s between the odds;

so let say user enters 3 an 12.
If I follow the logic of the above request to display the odd between the odds
then my output would be:
5,7,9
is this what question B is about?

woops! That should say "output the SUM of all the even numbers" between the 2 integers the user enters.
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 18 Jul, 2007 - 05:35 PM
Post #6


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


So your code here:
QUOTE
for(int n=firstNum;n<=secondNum;n++)

if(n%2==0)
sum += n;
else
//odd
{
cout<< "The sum of all even numbers between two numbers: " << sum << endl;
}


doesn't need the else statement.
Instead you can print the output that you want after the for loop something like:
CODE
for(int n=firstNum;n<=secondNum;n++)
{
  if(n%2==0)
     sum += n;
}

     cout<< "The sum of all even numbers between two numbers:  " << sum << endl;


And a for loop needs braces {} if a multiple statements need to be in that loop.
User is offlineProfile CardPM

Go to the top of the page

monkjessica
post 18 Jul, 2007 - 05:41 PM
Post #7


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 10


My Contributions


QUOTE(PennyBoki @ 18 Jul, 2007 - 06:35 PM) *

So your code here:
QUOTE
for(int n=firstNum;n<=secondNum;n++)

if(n%2==0)
sum += n;
else
//odd
{
cout<< "The sum of all even numbers between two numbers: " << sum << endl;
}


doesn't need the else statement.
Instead you can print the output that you want after the for loop something like:
CODE
for(int n=firstNum;n<=secondNum;n++)
{
  if(n%2==0)
     sum += n;
}


     cout<< "The sum of all even numbers between two numbers:  " << sum << endl;


And a for loop needs braces {} if a multiple statements need to be in that loop.



I tried that initally and also just copied and pasted your suggestion in and I keep getting a crazy answer of "2088810475" for the sum no matter what 2 integers are entered.


QUOTE(monkjessica @ 18 Jul, 2007 - 06:40 PM) *

QUOTE(PennyBoki @ 18 Jul, 2007 - 06:35 PM) *

So your code here:
QUOTE
for(int n=firstNum;n<=secondNum;n++)

if(n%2==0)
sum += n;
else
//odd
{
cout<< "The sum of all even numbers between two numbers: " << sum << endl;
}


doesn't need the else statement.
Instead you can print the output that you want after the for loop something like:
CODE
for(int n=firstNum;n<=secondNum;n++)
{
  if(n%2==0)
     sum += n;
}



     cout<< "The sum of all even numbers between two numbers:  " << sum << endl;


And a for loop needs braces {} if a multiple statements need to be in that loop.



I tried that initally and also just copied and pasted your suggestion in and I keep getting a crazy answer of "2088810475" for the sum no matter what 2 integers are entered.


Here is what my code looks like now.

CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{
    int firstNum;
    int secondNum;
    int n;
    int sum;
    
    cout << "Please enter two numbers." << endl;
    cout << "(First Number must be less than second number.)" << endl;
    cin >> firstNum >> secondNum;

    
    for(n=firstNum;n<=secondNum;n++)
{
if(n%2==0)
{
   //even number
}
else
  // odd number
{
cout << "The odd numbers are:  " << n << endl;

}
}
for(int n=firstNum;n<=secondNum;n++)
{
  if(n%2==0)
     sum += n;
}

     cout<< "The sum of all even numbers between two numbers:  " << sum << endl;




system ("PAUSE");
return 0;

}

User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 18 Jul, 2007 - 05:47 PM
Post #8


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


That is because n was declared already in the first for loop.
So the second loop

CODE
for(n=firstNum;n<=secondNum;n++)
User is offlineProfile CardPM

Go to the top of the page

monkjessica
post 18 Jul, 2007 - 05:56 PM
Post #9


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 10


My Contributions


QUOTE(PennyBoki @ 18 Jul, 2007 - 06:47 PM) *

That is because n was declared already in the first for loop.
So the second loop

CODE
for(n=firstNum;n<=secondNum;n++)




Still no luck. Do I need to prompt the user to re-enter 2 new numbers? Do I need to use a new value other than n for the second for statement?
User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 18 Jul, 2007 - 05:58 PM
Post #10


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


No and just try
instead int sum;
place this
int sum=0;

and repost your latest code so that I know what code you use.

This post has been edited by PennyBoki: 18 Jul, 2007 - 05:59 PM
User is offlineProfile CardPM

Go to the top of the page

monkjessica
post 18 Jul, 2007 - 06:05 PM
Post #11


New D.I.C Head

*
Joined: 18 Jul, 2007
Posts: 10


My Contributions


QUOTE(PennyBoki @ 18 Jul, 2007 - 06:58 PM) *

No and just try
instead int sum;
place this
int sum=0;

and repost your latest code so that I know what code you use.



Yeahhhhh! That worked! Thank you so much I've been working on this all day. This is a fast track course that has you doing a whole class in 3 weeks with help from the book alone so I am feeling overwhelmed.

The next set of instructions say "Output the number and their squares between 1 and 10." Any suggestions on how to get started with that?

Here is my code thus far:

CODE

#include <iostream>
#include <iomanip>

using namespace std;

int main ()

{
    int firstNum;
    int secondNum;
    int n;
    int sum=0;

    cout << "Please enter two numbers." << endl;
    cout << "(First Number must be less than second number.)" << endl;
    cin >> firstNum >> secondNum;

    
    for(n=firstNum;n<=secondNum;n++)
{
if(n%2==0)
{
   //even number
}
else
  // odd number
{
cout << "The odd numbers are:  " << n << endl;

}
}
for(n=firstNum;n<=secondNum;n++)
{
  if(n%2==0)
     sum += n;
}

     cout<< "The sum of all even numbers between your two numbers:  " << sum << endl;




system ("PAUSE");
return 0;

}



User is offlineProfile CardPM

Go to the top of the page

PennyBoki
post 18 Jul, 2007 - 06:16 PM
Post #12


system("revolution");

Group Icon
Joined: 11 Dec, 2006
Posts: 2,009



Thanked 5 times

Dream Kudos: 500

Expert In: Java,C++,C

My Contributions


Yeah, you can use another loop that will count to 10
inside the loop
print the number then print number*number

So the output would be:
1 - 1
2 - 4
3 - 9
4 - 16
.
.
.
9 - 81
10 - 100
Is this output fine?
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Closed TopicStart new topic
Time is now: 11/23/08 05:57AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month