Skip to content
ExamSpine

Lesson 1.10

Whole-number division and remainder

Part 1: Programs that talkReading, about 4 min3 exercises in this lesson, in Python2 exercises in this lesson, in Java

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

Whole-number division and remainder

By the end of this you will be able to split a number into a whole-number quotient and a remainder, and use both in one program.

Whole-number division

Dividing 17 by 5 gives 3 with 2 left over. Two operators give these two parts:

What it givesPythonJava17 and 5100 and 60
The quotient (how many whole times)/// between ints31
The remainder (what is left)%%240

In Python, // always gives a whole number. Plain / gives a decimal even when both sides are ints (17 / 5 is 3.4). In Java, / between two ints gives the whole-number quotient, the same as Python's //.

For numbers 0 or more, the remainder % gives the same answer in both languages: 17 % 5 is 2 because 5 * 3 + 2 = 17. For a negative number the two differ: -3 % 2 is 1 in Python and -1 in Java.

Splitting a value two ways

The quotient and remainder together split a value. Converting 100 minutes into hours and minutes:

minutes = 100
hours = minutes // 60
left = minutes % 60
print(str(hours) + " h "
    + str(left) + " min")
public static void main(String[] args) {
    int minutes = 100;
    int hours = minutes / 60;
    int left = minutes % 60;
    System.out.println(
        hours + " h " + left + " min");
}

When it runs:

1 h 40 min

The quotient says how many whole hours fit. The remainder says how many minutes are left. Together they give the complete answer.

A common mistake: using / in Python when you mean //. 100 / 60 gives 1.6666..., not 1. When you need a whole number, use //.

Now try it yourself on the questions below.

Exercise 1.10.1 · Trace itTrace: div mod

Complete the trace table for this Python code. [2]

Line 1: n = 47
Line 2: q = n // 10
Line 3: r = n % 10
Line 4: n = q
Line 5: print(r)
Line 6: print(n)

Fill in one row each time a line changes n, q or r, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rownqroutput
1
2
3
4
5
6

Exercise 1.10.21.10.1 · Write itMinutes to hours

Write a program that reads a number of minutes (a whole number, 0 or more) and prints it as hours and minutes on one line:

1 h 40 min
  1. The hours come from whole-number division by 60.
  2. The minutes left come from the remainder after dividing by 60.
  3. The format is the hours, then h , then the remaining minutes, then min.
  4. Print nothing else. Spaces at the end are ignored.

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

In Python, use // for whole-number division and % for the remainder. In Java, / between two ints already gives the whole number.

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 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • more than an hour (rules 1 to 4)

    You type

    1. typed: 100

    It prints

    1 h 40 min
  • less than an hour (rules 1 and 2)

    You type

    1. typed: 59

    It prints

    0 h 59 min
solution.pySolution.java

Exercise 1.10.31.10.2 · Write itCoin change

Write a program that reads a total in cents (a whole number, 0 or more) and prints how many 25-cent coins fit and how many cents are left, on one line:

3 coins, 12 left
  1. The number of coins comes from whole-number division by 25.
  2. The cents left come from the remainder after dividing by 25.
  3. The format is the coins, then coins, , then the cents left, then left.
  4. Print nothing else. Spaces at the end are ignored.

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

In Python, use // and %. In Java, / between ints and %.

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 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • coins and a remainder (rules 1 to 4)

    You type

    1. typed: 87

    It prints

    3 coins, 12 left
  • exactly one coin (rules 1 and 2)

    You type

    1. typed: 25

    It prints

    1 coins, 0 left
solution.pySolution.java