A company manufactures packing cartons in four sizes, i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes. Design a program to accept the number of boxes to be packed (N) by the user (maximum up to 1000 boxes) and display the break-up of the cartons used in descending order of capacity (i.e. preference should be given to the highest capacity available, and if boxes left are less than 6, an extra carton of capacity 6 should be used.)
import java.util.*;
public class Boxes {
int n;
void input() {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number of Boxes: ");
n = sc.nextInt();
if (n < 0 || n > 1000) {
System.out.println("Invalid Input");
System.exit(-1);
}
sc.close();
}
void denomination() {
int[] order = { 48, 24, 12, 6 };
for (int x = 0; x < order.length; x++) {
int v = n / order[x];
n -= order[x] * v;
if (n != 0 && order[x] == 6)
v++;
if (v == 0)
continue;
System.out.println(order[x] + " * " + v + " = " + order[x] * v);
}
}
public static void main(String[] args) {
Boxes a = new Boxes();
a.input();
a.denomination();
}
}