Thread: even and odd numbers

  1. #1
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49

    Thumbs up even and odd numbers

    in c++ how do you check if a number is odd or even?

    thanks

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    219
    num % 2

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is a FAQ entry with just that title. You may find that, and other useful stuff here:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    thanks !!

  5. #5
    C++Pandit
    Join Date
    Jul 2008
    Posts
    49
    how do i check if a number is not divisible by 2?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    not divisible by 2 == odd.

    Or you could read what % does.

    Or ... and so on.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The modulus operator (%) returns the remainder of a division. So say you divide 3 by 2. The remainder is 1. If the remainder is 0, the number must be even; otherwise, if it is odd, the remainder is not 0.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed