Print all even or odd integers

This is a discussion on Print all even or odd integers within the C++ Programming forums, part of the General Programming Boards category; I have to create a program that prints all odd or even integers between any two integers(inclusive) entered by the ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Print all even or odd integers

    I have to create a program that prints all odd or even integers between any two integers(inclusive) entered by the user.

    I'm sure I have to use some type of function for this, but which one? usually my teacher notes which funtion i have to use, but not this time.

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I think in this case your supposed to use ur own. Have you heard of the modulous (%) operator? You use this to find the 'remainder' of a number divided by another number. For example:
    5/3 is 1, with 2 left over (remainder). Doing 5%3 will give you 2 in the same way (I'm sure you know all about remainders etc, but im just saying it so you know what modulous does.

    So, if you take a number like 30, and do 30%2, it will give 0 because theres 0 remainders. If you did 31, it would give you 1 (remainder 1).

    If I was you, I'd ask for a start number, and end number, check that the end number is greater than the start number (or else there will be a never-ending loop), and then do the number %2, if its 1 I wont print, 0 i will print it (because its even).

    Is that clear or would you like an example?

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Yes, wev'e come across the modulus function already before. So I'm kind of familiar with it and understand the concept.

    However, I don't really fully understand your last paragraph. How do I check that the end number is greater than the starting number?
    In this case the user must decide what the start and ending number is.
    Could you provide me with an example to make things more clearer please?

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Ok, I was hoping you would ask
    Code:
    int StartNumber = 0;
    int EndNumber = 0;
    
    cout << "Select the start number: ";
    cin >> StartNumber;
    cout << "Select the end number: ";
    cin >> EndNumber;
    
    if (EndNumber < StartNumber)
    {
        cout << "End number is less than start number. Please try again";
        // either return here, or do something to allow them to chose the end number again. Up to you
    }
    
    int X = 0;   // X will be our counter
    
    // I assume you've done for loops?
    for (X = StartNumber; X <= EndNumber; X++)
    {
        if ((X%2) == 0) 
        {
            cout << X << endl;
        }
    }
    Now, I never checked any of this so there might be an error, but thats basically what I meant. It loops while the current number is less than or equal to the end number, and if its even it prints it.

    You have to check that EndNumber is greater that StartNumber, because if its not, can you see that X (since it starts at the StartNumber) will always be greater than the EndNumber, so the loop just wont run.

    However, It all depends how you do it. If the loop had said (X != EndNumber), it would work just the same if EndNumber was greater than startNumber, but if EndNumber was less, X would never equal EndNumber, and so it would be a never ending loop Hope I havent made it worse for you.

    ~ Paul

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Originally posted by stovellp
    Ok, I was hoping you would ask
    Code:
    int StartNumber = 0;
    int EndNumber = 0;
    
    cout << "Select the start number: ";
    cin >> StartNumber;
    cout << "Select the end number: ";
    cin >> EndNumber;
    
    if (EndNumber < StartNumber)
    {
        cout << "End number is less than start number. Please try again";
        // either return here, or do something to allow them to chose the end number again. Up to you
    }
    
    int X = 0;   // X will be our counter
    
    // I assume you've done for loops?
    for (X = StartNumber; X <= EndNumber; X++)
    {
        if ((X%2) == 0) 
        {
            cout << X << endl;
        }
    }
    Now, I never checked any of this so there might be an error, but thats basically what I meant. It loops while the current number is less than or equal to the end number, and if its even it prints it.

    You have to check that EndNumber is greater that StartNumber, because if its not, can you see that X (since it starts at the StartNumber) will always be greater than the EndNumber, so the loop just wont run.

    However, It all depends how you do it. If the loop had said (X != EndNumber), it would work just the same if EndNumber was greater than startNumber, but if EndNumber was less, X would never equal EndNumber, and so it would be a never ending loop Hope I havent made it worse for you.

    ~ Paul
    i feel your solution is inefficient.. instead of using % you can run a loop and incremeting the variable by 2 every time.. Thats a better alternative


    Code:
    # include <iostream.h>
    
    
    
    int main()
    {
    int StartNumber = 0;
    int EndNumber = 0;
    
    cout << "Select the start number: ";
    cin >> StartNumber;
    cout << "Select the end number: ";
    cin >> EndNumber;
    
    
    if (EndNumber < StartNumber)
    {
        cout << "End number is less than start number. Please try again";
        // either return here, or do something to allow them to chose the end number again. Up to you
    }
    
    
    for(int i=StartNumber;i<=EndNumber;i+=2)
    cout << i << endl;
    
    
    return 0;
    }

    this code will print all odd numbers if the start number is odd or even if the start number is even... this is more efficient way of looping since the lopp needs to run just half the number of times when compared to stovellp solution and does not need exta calculations to do the %(mod) stuff...

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    7
    Another method assuming a calculation will need to be made, which i don't believe has ben mentioned yet is to check if the first bit of the valut is set, which is equvilant to decimal 1, which causes a number to be odd. I expect this method would have some portability problems, and the correct mathematical method would indeed be the one already mentioned, but in a computing situation, this method may prove quicker in the context of integers.

    eg.

    8421

    1000 = 8+0+0+0 = 8
    1001 = 9+0+0+1 = 9

    In C, a simple (number & 1) statement using bitwise And to check the first bit would suffice.

    I felt this was worth mentioning, and also to serve as my first post here

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    you can also check to see if a number is odd:
    Code:
    if (num&1)
     //is odd
    
    if (!(num&1))
     //is even
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Thank you, your examples were very helpful, only, the user must decide wether only odd or only even numbers should be printed.

    I suppose I must use cout, but for example if the user presses 1 to display just even numbers and 2 for only odd, how do I connect their choice to the loop that will display only the even or odd numbers?

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Nice job people. I'm sure the poster learned alot about programming by having you post the solutions to his/her homework.
    Last edited by 7stud; 08-13-2003 at 05:06 AM.

  10. #10
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Hopefully Guti14 understands everything people have posted, and has used it to further his/her programming abilities. Hopefully.

    In my opinion efficiency should not be considered here, because it's such a simple program. The most logical way to go about it is to start with the first number, check if it's odd/even as you want, then print, adding two until the limit is reached. Checking bit states is a waste of time, because adding two to an integer is surely quicker under the hood then doing bitwise operations. Plus, why increment a number AND do a bitwise operation? And for that matter, a modulus operation?

    Just speculating here, but wouldn't modulus be the slowest method, as it involves dvision?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by 7stud
    Nice job people. I'm sure the poster learned alot about programming by having you post the solutions to his/her homework.
    Seeing it helps people learn, you cant just tell them what they need sometimes, u have to guide. Lighten up.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    RoD, you can guide without giving answers. you have to answer questions here with some care in detecting homework questions. Doing someone's homework isn't acceptable.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    I am not expecting people to do my homework here for me. Just some tips or hints as in what function I could/should use for instance, or give me some pseudocode. That's what my teacher does too sometimes, never the code, just some pseudocode.
    Occasionally I would like an example, sometimes it gets really difficult. Iv'e never done any programming before, and I had to dive straight into C++.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "I am not expecting people to do my homework here for me. Just some tips or hints"

    It's not your fault, and that's a good approach--unfortunately some posters just don't get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Print Randomly!
    By davewang in forum C Programming
    Replies: 3
    Last Post: 12-09-2008, 08:04 AM
  2. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 04:03 PM
  3. merging linked lists
    By scwizzo in forum C++ Programming
    Replies: 15
    Last Post: 09-14-2008, 05:07 PM
  4. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-20-2008, 11:58 PM
  5. print output problem
    By chor in forum C Programming
    Replies: 1
    Last Post: 12-29-2002, 08:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21