943,809 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3237
  • C++ RSS
Aug 30th, 2008
1

odd even numbers

Expand Post »
Hi there!
I am just starting out in c++ and so far, its going ok. I have a small problem in a simple program that is frustrating me, so I am hoping someone will point me in the right direction of where I've gone wrong.
The program basically prompts the user to input two integer values, then outputs all the odd numbers between these two integer values (and their squares), and also outputs all the even numbers between the two inputted values, along with the sum of these even numbers.
I have the program working sufficiently, so long as the first value entered by the user is smaller than the second value entered. Thats the small problem I've encountered; it should not make a difference between the size of the values entered. I think i need to change the condition in the for loop, but I've tried various conditions without success
Here is the program:

#include <iostream>
#include <conio.h>
#include <iomanip>

using namespace std;

int main()
{
    int a,b,a1,b2,square; //declaring variables
    int sum = 0;
    cout << "Enter two integers: " << endl;  //user inputs two integers
    cin >> a >> b;
    cout << "The numbers entered are: " << endl;  //displays the integers listed
    cout << "1. " << a << endl;
    cout << "2. " << b << endl;        
    a1 = a;
    b2 = b;
    //for odd numbers
    cout << "The odd numbers between " << a << " and " << b << 
           ", along with their squares are:" << endl;
    for (a++;a < b;a++)
    
    {
          if ((a%2) !=0)
          {
               cout << setw(4) << a;
               square = a * a;  //formula for finding the square of an integer
               cout << " = " << square;
          } //end if
    } //end for
    cout << endl << endl;
    
    //for even numbers
    cout << "The even numbers between " << a1 << " and " << b2 << 
          " are:" << endl;
    for (a1++;a1 < b2;a1++)
    {
       if((a1%2) == 0) 
       { 
           cout << setw(4) << a1; 
           sum += a1;  //formula for finding the sum of the even integers
       } //end if    
    } //end for
    cout << endl; 
    cout << "The total sum of all the even numbers is: " << sum;
    cout << endl << endl;
    getch();
    return 0;
}//end main
Similar Threads
Reputation Points: 60
Solved Threads: 0
Newbie Poster
bonnie1702 is offline Offline
17 posts
since Aug 2008
Aug 30th, 2008
0

Re: odd even numbers

After the cin statement if a < b then just swap them so that you don't have to code for that condition.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 30th, 2008
0

Re: odd even numbers

c++ Syntax (Toggle Plain Text)
  1. //--
Last edited by cikara21; Aug 30th, 2008 at 2:50 am.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Aug 30th, 2008
0

Re: odd even numbers

cikara21: Huh? Is that supposed to mean something?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Aug 30th, 2008
0

Re: odd even numbers

In both your 'for' loops your condition needs 'a' to be smaller than 'b', so before your 'for' loop but after your call for user input 'cin', you need a condition which checks the values entered, and then ensures that a is smaller than b

for example
//check to ensure that a is smaller than b
if(a > b)
{
	int temp = a;
	a = b;
	b = temp;
}
your code should now work fine even if a is greater than b
Reputation Points: 10
Solved Threads: 1
Newbie Poster
entei is offline Offline
6 posts
since Aug 2008
Sep 23rd, 2008
0

Re: odd even numbers

Hi all,

Just want to say thanks for your help, sorry it took so long to get back. I got the program working just fine.

Thanks again
n.m
Reputation Points: 60
Solved Threads: 0
Newbie Poster
bonnie1702 is offline Offline
17 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C and C++
Next Thread in C++ Forum Timeline: I have a problem in my assignment for binary trees





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC