THINK FIRST·CODE LATER

← All labs

Splitting the bill

Problem

Friends share a restaurant bill equally and add a tip. Write the algorithm in pseudocode, then the program SplitBill: read the bill, the tip in percent and the number of people; print the bill, the tip, the total and what each person pays, all with 2 decimals.

For the input

186.50 15 4

the program prints

Bill: 186.50
Tip (15%): 27.98
Total: 214.48
Each of 4 pays: 53.62

Trace your algorithm by hand for 100 10 4 before running it.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l2-5-data-1.txt

186.50 15 4
What your program must print for l2-5-data-1.txt
Bill: 186.50
Tip (15%): 27.98
Total: 214.48
Each of 4 pays: 53.62

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l2-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l2-5-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.