Project Answer: Number of boxes into cartons of capacities
☰Fullscreen
Table of Content:
Algorithm (Step-by-Step)
-
Start
-
Input number of boxes
N -
If
N <= 0orN > 1000, print "INVALID INPUT" and stop -
Calculate number of cartons in descending order:
-
c48 = N / 48 -
N = N % 48 -
c24 = N / 24 -
N = N % 24 -
c12 = N / 12 -
N = N % 12 -
c6 = N / 6 -
N = N % 6
-
-
If remaining boxes
N > 0, take one extra carton of 6 -
Display break-up
-
Display total cartons
-
End
Java Program
import java.util.Scanner; public class CartonPacking { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N; System.out.print("Enter number of boxes: "); N = sc.nextInt(); // Check for invalid input if (N <= 0 || N > 1000) { System.out.println("INVALID INPUT"); return; } int totalBoxes = N; int c48 = 0, c24 = 0, c12 = 0, c6 = 0; // 48 capacity cartons c48 = N / 48; N = N % 48; // 24 capacity cartons c24 = N / 24; N = N % 24; // 12 capacity cartons c12 = N / 12; N = N % 12; // 6 capacity cartons c6 = N / 6; N = N % 6; // If boxes left less than 6, use one extra carton of 6 if (N > 0) { c6 = c6 + 1; } int totalCartons = c48 + c24 + c12 + c6; // Display break-up if (c48 > 0) System.out.println("48 * " + c48 + " = " + (48 * c48)); if (c24 > 0) System.out.println("24 * " + c24 + " = " + (24 * c24)); if (c12 > 0) System.out.println("12 * " + c12 + " = " + (12 * c12)); if (c6 > 0) System.out.println("6 * " + c6 + " = " + (6 * c6)); System.out.println("Remaining boxes = 0"); System.out.println("Total number of boxes = " + totalBoxes); System.out.println("Total number of cartons = " + totalCartons); sc.close(); } }
Sample Run
Input:
Enter number of boxes: 726
Output:
48 * 15 = 720 6 * 1 = 6 Remaining boxes = 0 Total number of boxes = 726 Total number of cartons = 16