The remainder operator (%) is very useful in coding. The following Boolean expressions can be used to test whether a number is even or odd by seeing if there is a remainder when it is divided by 2 or if it is divisible by another number:
Note 2.2.3.
A warning: because Java’s % is a remainder operator and not a true mathematical modulo operator, you can’t check if a number is odd with the expression num % 2 == 1.
That expression will be true if num is positive and odd and false when num is even, both of which are correct. But if num is negative and odd, its remainder when divided by 2 is -1, not 1 and this expression will evaluate to false. Thus you should always use num % 2 != 0 to check if num is odd.