Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A box of cookies can hold 24 cookies and a container can hold 75 boxes of cookie

ID: 3786533 • Letter: A

Question

A box of cookies can hold 24 cookies and a container can hold 75 boxes of cookies. Write a program that prompts the user to enter the total number of cookies, then outputs the number of boxes and the number of containers to ship the cookies. If there are not enough cookies to fill a box, the box can be discarded and the number of leftover cookies should be output. If the number of boxes will not fill a container, then the leftover boxes can be discarded and the number of leftover boxes should be output.

This is for Java.

Explanation / Answer

// Cookies.java
import java.util.Scanner;

public class Cookies
{
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);

       int totalCookies, cookiesBoxes ,extraCookies, containerBoxes, extraBoxes;

System.out.println("Enter total number of cookies: ");
totalCookies = sc.nextInt();

System.out.println(" Total number of cookies: " + totalCookies);

   cookiesBoxes = totalCookies / 24;
   extraCookies = totalCookies % 24;
   containerBoxes = cookiesBoxes / 75;
   extraBoxes = cookiesBoxes % 75;


   if (cookiesBoxes > 0)
   System.out.println("Boxes needed: " + cookiesBoxes);
   if (containerBoxes > 0)
   System.out.println("Containers needed: " + containerBoxes);
   if (extraBoxes > 0)
   System.out.println("Leftover boxes: " + extraBoxes);
   if (extraCookies > 0)
   System.out.println("Leftover cookies: " + extraCookies);  
   }
}

/*
output:

Enter total number of cookies:
2500

Total number of cookies: 2500
Boxes needed: 104
Containers needed: 1
Leftover boxes: 29
Leftover cookies: 4


*/