Skip to content
ExamSpine

Lesson 2.5

Remainder inside a condition

Part 2: DecisionsReading, about 3 min2 exercises in this lesson

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Remainder inside a condition

By the end of this you will be able to use the remainder operator inside a condition to check divisibility.

You already know that % gives the remainder. A common pattern is to put it inside a condition: n % 3 == 0 asks whether n is divisible by 3. If the remainder is 0, nothing is left over, so the number divides evenly.

Checking divisibility

To check whether a number is even, ask whether its remainder after dividing by 2 is 0:

n = int(input())
if n % 2 == 0:
    print("Even")
else:
    print("Odd")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    int n
        = Integer.parseInt(
            scanner.nextLine());
    if (n % 2 == 0) {
        System.out.println("Even");
    } else {
        System.out.println("Odd");
    }
}

This test is right for negative numbers too: a remainder of 0 means divisible in both languages. A test for odd numbers needs more care. In Java, -3 % 2 is -1, so n % 2 == 1 would miss negative odd numbers. Putting odd numbers in the else, as the example does, avoids that.

The pattern works for any divisor: n % 5 == 0 checks divisibility by 5, year % 4 == 0 checks divisibility by 4 (useful for leap years).

A common mistake: writing n / 3 == 0 instead of n % 3 == 0. Division gives the result of dividing, not what is left over. For 7, 7 / 3 is about 2.33 in Python and 2 in Java (which drops the fraction between ints). Neither says whether anything is left. Only the remainder tells you about divisibility.

Now try it yourself on the questions below.

Exercise 2.5.12.5.1 · Write itFizz check

Write a program that reads a whole number (0 or more) and checks whether it is divisible by 3.

  1. Always print Number: followed by the number on the first line.
  2. If the number is divisible by 3 (its remainder after dividing by 3 is 0), print Fizz on the next line.
  3. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • divisible by 3 (rules 1 and 2)

    You type

    1. typed: 9

    It prints

    Number: 9
    Fizz
  • not divisible by 3 (rule 1 only)

    You type

    1. typed: 7

    It prints

    Number: 7
  • zero is divisible by 3 (rules 1 and 2)

    You type

    1. typed: 0

    It prints

    Number: 0
    Fizz
solution.pySolution.java

Exercise 2.5.22.5.2 · Write itLeap year check

Write a program that reads a year (a whole number, 1 or more) and checks whether it is a leap year.

  1. Always print Year: followed by the year on the first line.
  2. A year is a leap year if it is divisible by 4 (its remainder after dividing by 4 is 0).
  3. If it is a leap year, print Leap year on the next line. Otherwise, print Not a leap year.
  4. Spaces at the end of a line are ignored.

This is a simplified rule. The real rule also checks 100 and 400, but this exercise uses only 4.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.

  • divisible by 4: a leap year (rules 1 to 3)

    You type

    1. typed: 2024

    It prints

    Year: 2024
    Leap year
  • not divisible by 4 (rules 1 and 3)

    You type

    1. typed: 2023

    It prints

    Year: 2023
    Not a leap year
  • the smallest leap year above 0 (rule 2)

    You type

    1. typed: 4

    It prints

    Year: 4
    Leap year
solution.pySolution.java