Results 1 to 6 of 6

Thread: C: is a number even!!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99

    C: is a number even!!

    how can i check if a number is even in C...is there a function to do so, if so which header file is it in?


    Thanks,
    Steve

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    if(num%2==0)
    //then the number's even

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    thanks

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    just use macros like
    Code:
    #define iseven(c) (c%2==0)
    usage
    Code:
    int a;
    ........
    if (iseven(a) ) ..........;

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    (num & 0x1) would be faster, accomplishing the same.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Now that's a smart one... have to keep that one in mind!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width