Thread: Quick question, should be simple to answer.

  1. #1
    Registered User
    Join Date
    Jan 2006

    Quick question, should be simple to answer.

    Hello. I recently started taking a class on C programming, and am stuck. One part of the program needs to tell the user if the number they entered is odd or even. How would I go about doing this? I was told to look at the remainder, but am still confused on how to make this work. Any help would be greatly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    An odd number will have a remainder when divided by 2 an even number won't. To get the remainder use the % operator.
    if (Number %2)

  3. #3
    Registered User
    Join Date
    Jan 2006
    That is all I need to do? I've been trying many different things, such as
    if ((number % 2) == (number / 2))

    I did not realize that it was so simple. Thanks a lot for your help!

  4. #4
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    West Virginia
    if (number & 1) { /* number is odd */ }
    Assuming that number is an integer type, this works. I'll leave it as an exercise for you to figure out why.
    Insert obnoxious but pithy remark here

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    There is an FAQ entry on the subject:

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question
    By surfingbum18 in forum C Programming
    Replies: 5
    Last Post: 12-04-2007, 06:16 AM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Quick question if i may
    By badboy16z in forum C++ Programming
    Replies: 1
    Last Post: 10-20-2003, 10:41 PM
  4. Quick simple question
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2003, 08:37 AM
  5. simple answer? test your IQ
    By Fountain in forum A Brief History of
    Replies: 12
    Last Post: 07-11-2002, 04:32 PM