top of page
10. Determine if a number is odd or even
Description
The computation checks a number variable, and returns True if the number is even and False if it is odd
Code
IF ROUND( ( NumVar-n / 2 ), 0 ) * 2 = NumVar-n
TRUE
ELSE
FALSE
END IF
Alternative:
IF ( NumVar-n / 2 ) = ROUND( ( NumVar-n / 2 ), 0 )
TRUE
ELSE
FALSE
END IF
Explanation
Both computations return TRUE or FALSE, where TRUE = even number, and FALSE = odd number.
An odd number divided by two will produce a decimal. If we trim the decimal off with ROUND and multiply the integer portion by two, it will be less than the original number. For example: 5 / 2 = 2.5. 2 * 2 = 4. Even numbers, on the other hand, will always equal the original number when put through the same formula: 4 / 2 = 2. 2 * 2 = 4.
bottom of page