Solution Design You might not know how to write code that finds out if a number is odd or even. This kind of
exercise is easy to solve if you already know how to solve it, but difficult if you have to reinvent the
solution yourself. Feel free to look up answers to questions you have on the internet. Include the
programming language’s name for more specific results, such as ―python find out if a number is odd or even‖. The question-and-answer website https://stackoverflow.com is usually at the top of the search
results and reliably has direct, high-quality answers. Looking up programming answers online isn’t
―cheating.‖ Professional software developers look up answers dozens of times a day!
The % modulo operator can mod a number by 2 to determine its evenness or oddness. The
modulo operator acts as a sort of ―division remainder‖ operator. If you divide a number by 2 and the
remainder is 1, the number must be odd. If the remainder is 0, the number must be even. For
example, 42 % 2 is 0, meaning that 42 is even. But 7 % 2 is 1, meaning that 7 is odd.
Floating-point numbers such as 3.1415 are neither odd nor even, so both isOdd() and
isEven()
should return False for them.
Keep in mind that the isOdd() and isEven() function you write must return a Boolean True
or False value, not an integer 0 or 1. You need to use a == or != comparison operator to produce a
Boolean value: 7 % 2 == 1 evaluates to 1 == 1, which in turn evaluates to True.
Special Cases and Gotchas Non-integer numbers such as 4.5 or 3.1415 are neither odd nor even, so both isOdd() and
isEven()
should return False for them.
Now try to write a solution based on the information in the previous sections. If you still have
trouble solving this exercise, read the Solution Template section for additional hints.
Solution Template Try to first write a solution from scratch. But if you have difficulty, you can use the following