Java Arena
Step 2 of 6

The remainder operator %

% gives the remainder of a division between whole numbers: 17 % 5 is 2, because 17 is three fives and 2 left over.

System.out.println(17 % 5);
System.out.println(12 % 4);
System.out.println(7 % 10);
Output
2
0
7

A remainder of 0 means the division comes out even, so % is the way to test whether a number divides by another. The most common case is even and odd: a number is even when number % 2 == 0.

Negative numbers

In Java the remainder has the same sign as the number being divided, so -7 % 2 is -1, not 1:

System.out.println(-7 % 2);
System.out.println(-8 % 2);
Output
-1
0

That's why "is it odd?" is safest written as number % 2 != 0: number % 2 == 1 is false for odd negative numbers.

Your task

Challenge 1 of 3

Read a whole number and print the remainder when it's divided by 3. For the input 11 the program prints:

11 % 3 = 2

Challenge 2

Read a whole number and print Even or Odd. For the input 6 the program prints:

Even

Challenge 3

Read a whole number and print whether it divides by 7. For the input 21 the program prints:

21 is divisible by 7

Otherwise it prints, for example, 20 is not divisible by 7. Hidden tests include negative numbers.