To check if a number is even in Java, do this:
number % 2 === 0
.number % 2
returns the remainder of division by 2
. Thus if the remainder is 0
, then the number
is even.
int evenNumber = 4;
int oddNumber = 3;
if (evenNumber % 2 === 0) {
System.out.println("This will be printed!");
}
if (oddNumber % 2 === 0) {
System.out.println("This will NOT be printed!");
}
if (8 % 2 === 0) {
System.out.println("This will also be printed!");
}