Lesson 1.11 · Write it
Split bill
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 bill (a whole number) on the first line and the number of people (a whole number, at least 1) on the second line, and prints each person's share on one line.
- The share is the total divided by the number of people, keeping the fraction.
- Print only the share, as a number. Spaces at the end are ignored.
Your program is run several times. Only what it prints is checked.
In Python, plain / keeps the fraction. In Java, at least one side of the / must be a double: cast the total with (double) before dividing.
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.
a total that does not halve evenly (rule 1)
You type
- typed: 9
- typed: 4
It prints
2.25
a total that does not divide evenly (rule 1)
You type
- typed: 10
- typed: 4
It prints
2.5