Results 1 to 16 of 16

Thread: Finding if a number is even or odd, in C

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Posts
    3

    Question Finding if a number is even or odd, in C

    Hi,

    This is actually a C question, but I couldn't find a math section on codeguru's forums, so this is pretty much the only place I could find a math section. Anyways, we are supposed to write a recursive function that checks if the given parameter is even and returns 1 if it is, otherwise if it is odd, it will return 0. This is easy, except I cannot use division or modulo.

    I was hoping someone could tell me the formula or the code for this. I have been racking my brains all day.

    Thanks in advance,

    John

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Use bit logic:

    (Number And 1) = 0 Then Even Else Odd
    Last edited by DiGiTaIErRoR; Nov 27th, 2003 at 07:04 PM.

  3. #3
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Or in C:

    PHP Code:
    inline int IsEven (int x)
    {
        if ((
    1) == 0) return 1;
        return 
    0;

    EDIT 1:
    Wait... recursion?! Why would you want do multiple steps when it can be done in one? Did you consider the bit-shift operators? Technically it's division but not in the same sense...

    EDIT 2:
    Wait, perhaps if you wanted to calculate the number of 1-bits in an integer, you'd go:

    PHP Code:
    inline int HasEvenBits (int x);
    {
        
    int bitcount 0;
        
    int counter sizeof (int) * 1;
        while (
    counter--)
        {
            
    >>= 1;
            
    bitcount += (1);
        }
        if ((
    bitcount 1) == 0) return 1;
        return 
    0;

    Last edited by Dreamlax; Nov 27th, 2003 at 09:45 PM.

  4. #4
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    In binary if a number is odd the right bit(big endian) is 1, as this is the only 1 in binary. All the other bits represent powers of 2 which are of course, even.

    1 = 1
    10 = 2
    11 = 3
    100 = 4
    101 = 5
    110 = 6
    111 = 7
    1000 = 8
    10000 = 16
    100000 = 32
    1000000 = 64
    10000000 = 128
    11111110 = 254

    The only way a number can be odd, is if it has the plus one.

  5. #5
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hmmm

    would this work:

    Code:
    inline bool IsEven (int x)
    {
        return ( ( ( x >> 1 ) << 1 ) == x);
    }
    I know the AND one is better, but this looks nice. (however, not sure if it would work)

    BTW, a recursinve one:
    Code:
    bool IsEven(int x)
    {
        int absX = (x < 0) ? -1*x : x; // get absolute value
        return (absX > 1) ? !IsEven(absX-3) : (absX==0);
    }
    I haven't checked this, but it should *work*.
    BTW, does C have () ? : ; ability. if not, can rewrite easily...
    sql_lall

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    inline bool IsEven (int x){
    return !(x & 1);
    }
    can't get any simpler than that
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Only problem with kedaman's is that it does not entirely fit his requests. It must return 1, not true, if it is even :P. Just me being a fussy boots.

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    true, and then we would have to put in the recursion somehow as well
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  9. #9
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Code:
    int IsEven (int x)
    {
        if (x == 1) return 0;
        if (x == 0) return 1;
        return (IsEven (x - 2));
    }
    Just don't blame me when you get a stack overflow .

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2003
    Posts
    3

    Talking Thanks for replying

    Hi,
    I didn't except this many reply but they really did help. Since its a C program, I am thinking of doing it something like this:

    Step 1. Gets the number (lets call it nNum)
    Step 2. Starts from 0 and starts adding 2 (lets call this variable nEvenOdd)

    Every time it will check If nEvenOdd < nNum, and if so it will continue adding 2. Else If nEvenOdd > nNum it will return 0, Else If nEvenOdd = nNum, it will return 1.

    In theory if the nNum is say 16, then 0 + 2 + 2 + 2....= 16, therefore meaning that nNum = nEvenOdd, therefore even.

    Any suggestions to iprove this theory.

    John

  11. #11
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    in c/c++

    if (x%2==0)
    even
    else
    odd
    Regards

  12. #12
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    i think 0 is a special case
    Regards

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    sw_is_great: read the question before you post

    jcgalang: go with dreamlax one, it is the same thing but top down rather than bottom up (recursion is most intuitively top-down) start with something asked, and then go toward the special case.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    sorry boss

    i went by the subject name
    Regards

  15. #15
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    So... you can't use division, what's stopping you from:

    VB Code:
    1. If CInt(Num * .5) * 2 = Num Then do_something

    Where Num is an integer.

  16. #16
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Perhaps because that isn't recursive, but still, any function must be more efficient than the one I wrote.

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