Thread: Odd and even

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Odd and even

    I am attempting to create a little sub program that reads a number rrom
    the user then prints if it is odd or even. This is what I have come up with, but it keeps printing the number is odd. Is there anything I can do to change this?

    Code:
    int main ( void )
    {
       int n;
     	 
       cout << "Enter a number: ";
       cin >> n;
     	 
       if (( n % n ) / 2 )
          cout << "\nThe number is even" << endl;
      	 
        cout << "\nThe number is odd" << endl;
     	 
        cin.get();  // freeze console window
        cin.ignore();
        
        return 0;   // indicate program ended sucsessfully
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    if (( n % n ) / 2 )
    Work that out in your head. A number divided by itself is always 1 with no remainder. Try just n % 2.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Lol. sorry, its ok I just solved it.

    My if statement was almost right, I changed it to

    Code:
    if (( n / 2 ) % n )
    and it works ok now. Thanks isme86 for the reply, I need my brain exchanged for a new one!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if (( n / 2 ) % n )
    That formula is incorrect, are you sure it works ok now? Why not use n % 2? Also, you are missing an else in your code.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    The correct code is. Your code isn't working like you think it should, it isn't comparing two values, just evaluating a statment and feeding it into an if statement.
    Code:
    if((n % 2) == 0)
    that means if a number n divided by 2 has a remainder of zero, it is even. Otherwise it is odd.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Thanks guys. I really should learn to think formula's through before I rush ahead and input them. Great advise, I'l remember your tips for next time too.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Apart from the slight problem of the lack of else statement.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Salem
    Not only remarkable, but also wrong.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    LOL - so it is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah... somebody needs to fix that... and make a C++ version... introducing C code to somebody trying to learn C++ can send confusing and mixed messages... even though the algorithm is the same.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    There should be a fully C++ FAQ and a Fully C FAQ.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by major_small
    yeah... somebody needs to fix that...
    Yeah, that's been the state of it for a while.
    http://cboard.cprogramming.com/showthread.php?t=82195
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed