go to previous page   go to home page   go to next page hear noise highlighting

Answer:

One. This is a common programming trick. Often you need to know the oddness and evenness of an integer.


Remainder with Negative Integers

The remainder operator can be used with negative integers. The rule is:

  1. Perform the operation as if both operands were positive.
  2. If the left operand is negative, then make the result negative.
  3. If the left operand is positive, then make the result positive.
  4. Ignore the sign of the right operand in all cases.

For example:

17 %  3 == 2     -17 %  3 == -2     
17 % -3 == 2     -17 % -3 == -2

From the two pieces (the quotient and the remainder) the original integer can be put back together:

quotient * divisor + remainder arrow theInteger

For example,

-17 / 3 == -5  ;   -17 %  3 == -2  

(-5) * 3 + (-2) = -15 + (-2) = -17  

You may wish to practice with the following:






Expression ResultExpression Result
7 -3 -7 5
-10 5 10 -6
-129 100 -1999 -100
-17 2 -18 2

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?


go to previous page   go to home page   go to next page