THINK FIRST·CODE LATER

← All labs

Egg cartons: whole numbers and remainders

Problem

A farm packs eggs in cartons of 12. Write EggCartons: read a number of eggs and print how many cartons are full, how many eggs are left over, how many cartons are needed to carry all the eggs, and how many places stay empty.

For the input

100

the program prints

Eggs: 100
Full cartons: 8
Eggs left over: 4
Cartons needed to carry all: 9
Empty places: 8

Find a formula for "cartons needed" that works without any if — also when the number of eggs is an exact multiple of 12.

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-6-data-1.txt

100
What your program must print for l2-6-data-1.txt
Eggs: 100
Full cartons: 8
Eggs left over: 4
Cartons needed to carry all: 9
Empty places: 8

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-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l2-6-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.