Answer:
One. This is a common programming trick. Often you need to know the oddness and eveness of an integer.
Remainder with Negative Integers
The remainder operator can be used with negative integers. The rule is:
- Perform the operation as if both operands were positive.
- If the left operand is negative, then make the result negative.
- If the left operand is positive, then make the result positive.
- Ignore the sign of the right operand in all cases.
For example:
17 % 3 == 2 -17 % 3 == -2 17 % -3 == 2 -17 % -3 == -2
You may wish to practice with the following:
QUESTION 17:
Five pirates find a chest of 123 gold coins and wish to divide the 123 coins evenly amoung themselves. How many coins does each pirate get?
The parrot gets any leftover coins. How many coins does the parrot get?