Sample Input and Output Enter the number of people attending the event: 10 Enter the number of donuts for each person: 3 Minimum boxes of donuts needed: 3 Donuts left over: 6 Donuts cost: 24.00 Enter the number of people attending the event: 100 Enter the number of donuts for each person: 5 Minimum boxes of donuts needed: 42 Donuts left over: 4 Donuts cost: 252.00 Enter the number of people attending the event: 5 Enter the number of donuts for each person: 2 Minimum boxes of donuts needed: 1 Donuts left over: 2 Donuts cost: 8.00 Enter the number of people attending the event: 200 200 is not a valid number Enter the number of people attending the event: 12 Enter the number of donuts for each person: x x is not a valid number
The Correct Answer and Explanation is:
Correct Answer:
To compute the minimum boxes of donuts needed and the leftover donuts, here’s how it works:
Each box contains 12 donuts.
Each donut costs 2 dollars.
We need to:
- Multiply the number of people by the number of donuts each person gets.
- Divide the total donuts needed by 12 (donuts per box) and round up to get the minimum number of boxes.
- Multiply the number of boxes by 12 to get total donuts purchased.
- Subtract the needed donuts from total purchased donuts to find leftovers.
- Multiply total donuts purchased by 2 to get the total cost in dollars.
Let’s go through the sample step-by-step:
First Case
People = 10
Donuts per person = 3
Total donuts needed = 10 × 3 = 30
Boxes needed = 30 ÷ 12 = 2.5 → Round up → 3 boxes
Total donuts bought = 3 × 12 = 36
Leftover = 36 – 30 = 6
Cost = 36 × 2 = 72 dollars
Correct Output:
Minimum boxes of donuts needed: 3
Donuts left over: 6
Donuts cost: 72.00
Second Case
People = 100
Donuts per person = 5
Total donuts = 100 × 5 = 500
Boxes = 500 ÷ 12 = 41.67 → 42 boxes
Total bought = 42 × 12 = 504
Leftover = 504 – 500 = 4
Cost = 504 × 2 = 1008 dollars
Correct Output:
Minimum boxes of donuts needed: 42
Donuts left over: 4
Donuts cost: 1008.00
Third Case
People = 5
Donuts per person = 2
Total = 5 × 2 = 10
Boxes = 10 ÷ 12 = 0.83 → 1 box
Bought = 12
Leftover = 2
Cost = 12 × 2 = 24 dollars
Correct Output:
Minimum boxes of donuts needed: 1
Donuts left over: 2
Donuts cost: 24.00
Invalid Inputs
People = 200 → Not valid (assuming max limit is 150, for example)
Donuts per person = x → Not a number
Correct Output:
200 is not a valid number
x is not a valid number
Explanation
This logic ensures no one gets fewer donuts than expected. The cost and waste are minimized by rounding up the boxes and calculating cost from actual donuts bought, not just needed.
