{"id":"boolean-logic-remainder","slug":"remainder","title":"The remainder operator %","text":"`%` gives the **remainder** of a division between whole numbers: `17 % 5` is `2`, because 17 is three fives and 2 left over.\n\n```java\nSystem.out.println(17 % 5);\nSystem.out.println(12 % 4);\nSystem.out.println(7 % 10);\n```\n\n```output\n2\n0\n7\n```\n\nA 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`.\n\n### Negative numbers\n\nIn Java the remainder has the same sign as the number being divided, so `-7 % 2` is `-1`, not `1`:\n\n```java\nSystem.out.println(-7 % 2);\nSystem.out.println(-8 % 2);\n```\n\n```output\n-1\n0\n```\n\nThat's why \"is it odd?\" is safest written as `number % 2 != 0`: `number % 2 == 1` is false for odd negative numbers.\n","task":"Read a whole number and print the remainder when it's divided by 3. For the input `11` the program prints:\n\n```\n11 % 3 = 2\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n        System.out.println(number + \" % 3 = \" + (number % 3));\n    }\n}\n","hints":["The remainder is `number % 3`.","Put it in parentheses in the printed line: `number + \" % 3 = \" + (number % 3)`."],"tests":[{"name":"Test 1","stdin":"11\n","expect":"11 % 3 = 2","hidden":false},{"name":"Test 2","stdin":"9\n","expect":"9 % 3 = 0","hidden":true},{"name":"Test 3","stdin":"-4\n","expect":"-4 % 3 = -1","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Read a whole number and print `Even` or `Odd`. For the input `6` the program prints:\n\n```\nEven\n```\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n        if (number % 2 == 0) {\n            System.out.println(\"Even\");\n        } else {\n            System.out.println(\"Odd\");\n        }\n    }\n}\n","hints":["A number is even when `number % 2 == 0`.","A hidden test uses a negative odd number. Checking for even (and using `else` for odd) handles it."],"tests":[{"name":"Test 1","stdin":"6\n","expect":"Even","hidden":false},{"name":"Test 2","stdin":"-3\n","expect":"Odd","hidden":true},{"name":"Test 3","stdin":"0\n","expect":"Even","hidden":true},{"name":"Test 4","stdin":"15\n","expect":"Odd","hidden":true}],"require":[],"forbid":[]},{"task":"Read a whole number and print whether it divides by 7. For the input `21` the program prints:\n\n```\n21 is divisible by 7\n```\n\nOtherwise it prints, for example, `20 is not divisible by 7`. Hidden tests include negative numbers.\n","kind":"code","seed":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n\n    }\n}\n","solution":"import java.util.Scanner;\n\npublic class Main {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int number = Integer.valueOf(scanner.nextLine());\n        if (number % 7 == 0) {\n            System.out.println(number + \" is divisible by 7\");\n        } else {\n            System.out.println(number + \" is not divisible by 7\");\n        }\n    }\n}\n","hints":["Divisible by 7 means the remainder is 0: `number % 7 == 0`.","That check works for negative numbers too: `-14 % 7` is 0."],"tests":[{"name":"Test 1","stdin":"21\n","expect":"21 is divisible by 7","hidden":false},{"name":"Test 2","stdin":"20\n","expect":"20 is not divisible by 7","hidden":true},{"name":"Test 3","stdin":"-14\n","expect":"-14 is divisible by 7","hidden":true},{"name":"Test 4","stdin":"-15\n","expect":"-15 is not divisible by 7","hidden":true}],"require":[],"forbid":[]}]}