Skip to content
ExamSpine

Lesson 1.10 · Write it

Coin change

A whole programPython or Java5 tests, 3 of them hidden

On a phone? Put the lines in order instead

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