Bakery tray packing
A bakery cools its loaves on trays that hold exactly 6 loaves each. At the end of a bake the head baker counts the loaves and wants to know two things: how many trays are completely full, and how many loaves are left on the counter waiting for the next tray.
Read the number of loaves and print both values in the format shown.
Input
One line containing a single integer n, the number of loaves baked.
Output
Two lines: Trays: t where t is the number of full trays, then Left over: r where r is the number of loaves that do not fill a tray.
Example 1
Input
20
Output
Trays: 3 Left over: 2
20 loaves fill 3 trays (18 loaves) and 2 loaves remain.
Example 2
Input
5
Output
Trays: 0 Left over: 5
Five loaves are not enough for a single tray, so all five are left over.
Constraints
- 0 <= n <= 100000
Hints
Hint 1 of 3
When both operands are int, the / operator throws away the fractional part, which is exactly "how many whole trays".
Hint 2 of 3
The % operator gives the remainder of that division: what is left after the full trays are taken away.
Hint 3 of 3
Print with string interpolation: Console.WriteLine($"Trays: {loaves / 6}");.
Solution
Show a reference solution and explanation
int loaves = int.Parse(Console.ReadLine()!);
int trays = loaves / 6;
int leftOver = loaves % 6;
Console.WriteLine($"Trays: {trays}");
Console.WriteLine($"Left over: {leftOver}");
Why it works
Integer division in C# truncates towards zero, so loaves / 6 is the number of complete trays, and loaves % 6 is the remainder that did not fit. The two operators always agree: (loaves / 6) * 6 + loaves % 6 gives back loaves. Reading the input with int.Parse converts the text line into a number before any arithmetic happens; without that step / would not even compile on a string.
Lesson for this exercise: Variables and Types in C#