Shop OBEX P1 Docs P2 Docs Learn Events
Display even(odd) number only — Parallax Forums

Display even(odd) number only

Mohamed RefkyMohamed Refky Posts: 47
edited 2006-03-22 17:04 in BASIC Stamp
Hello,
How to determine that a number is odd number or even number?

Thank You

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-03-22 16:47
    IF X//2 == 1
    'number is odd
    ELSE
    'number is even
    ENDIF

    you can also do it by testing bit 0 of the value, it its a 0 its even if its a 1 its odd, this method is actually·quicker than using modulo in the first method.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-03-22 16:49
    Here's how to implement Paul's BIT0 suggestion:

    · IF (myValue.BIT0 = 1) THEN
    ··· ' number is odd
    · ELSE
    ··· ' number is even
    · ENDIF

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-03-22 17:04
    Thanks Jon, I see my work with the propeller is starting to crowd out PBASICs syntax, as my above IF-THEN example has some syntax errors.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
Sign In or Register to comment.