Quick question, should be simple to answer.

This is a discussion on Quick question, should be simple to answer. within the C Programming forums, part of the General Programming Boards category; Hello. I recently started taking a class on C programming, and am stuck. One part of the program needs to ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    26

    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
    Posts
    847
    An odd number will have a remainder when divided by 2 an even number won't. To get the remainder use the % operator.
    Code:
    if (Number %2)

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    26
    That is all I need to do? I've been trying many different things, such as
    Code:
    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
    Location
    West Virginia
    Posts
    235
    Code:
    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
    Location
    Canada
    Posts
    8,047
    There is an FAQ entry on the subject: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    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, 05: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, 07:37 AM
  5. simple answer? test your IQ
    By Fountain in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 07-11-2002, 04:32 PM

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