KnowledgeBoat Logo

Computer Science

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.)

Test your program with the following data and some random data:

Example 1

INPUT:
N = 726

OUTPUT:
48 * 15 = 720
6 * 1 = 6
Remaining boxes = 0
Total number of boxes = 726
Total number of cartons = 16

Example 2

INPUT:
N = 140

OUTPUT:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6 * 1 = 6
Remaining boxes = 2 * 1 = 2
Total number of boxes = 140
Total number of cartons = 6

Example 3

INPUT:
N = 4296

OUTPUT:
INVALID INPUT

Java

Java Iterative Stmts

ICSE Prac 2017

25 Likes

Answer

import java.util.Scanner;

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

        System.out.print("Enter number of boxes (N): ");
        int n = in.nextInt();
        
        if (n < 1 || n > 1000) {
            System.out.println("INVALID INPUT");
            return;
        }

        int cartonSizes[] = {48, 24, 12, 6};

        int total = 0;
        int t = n;
        for (int i = 0; i < cartonSizes.length; i++) {
            int cartonCount = t / cartonSizes[i];
            t = t % cartonSizes[i];
            total += cartonCount;
            if (cartonCount != 0) {
                System.out.println(cartonSizes[i] + " * " + cartonCount + 
                    " = " + (cartonSizes[i] * cartonCount));
            }
        }

        /*
         * This if check is for the case when
         * boxes left are less than 6. We need
         * one more carton of capacity 6 in this
         * case so total is incremented by 1.
         */
        if (t != 0) {
            System.out.println("Remaining boxes = " + t 
                + " * 1 = " + t);
            total++;
        }
        else {
            System.out.println("Remaining boxes = 0");
        }

        System.out.println("Total number of boxes = " + n);
        System.out.println("Total number of cartons = " + total);
    }
}

Output

BlueJ output of 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.) Test your program with the following data and some random data: Example 1 INPUT: N = 726 OUTPUT: 48 * 15 = 720 6 * 1 = 6 Remaining boxes = 0 Total number of boxes = 726 Total number of cartons = 16 Example 2 INPUT: N = 140 OUTPUT: 48 * 2 = 96 24 * 1 = 24 12 * 1 = 12 6 * 1 = 6 Remaining boxes = 2 * 1 = 2 Total number of boxes = 140 Total number of cartons = 6 Example 3 INPUT: N = 4296 OUTPUT: INVALID INPUTBlueJ output of 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.) Test your program with the following data and some random data: Example 1 INPUT: N = 726 OUTPUT: 48 * 15 = 720 6 * 1 = 6 Remaining boxes = 0 Total number of boxes = 726 Total number of cartons = 16 Example 2 INPUT: N = 140 OUTPUT: 48 * 2 = 96 24 * 1 = 24 12 * 1 = 12 6 * 1 = 6 Remaining boxes = 2 * 1 = 2 Total number of boxes = 140 Total number of cartons = 6 Example 3 INPUT: N = 4296 OUTPUT: INVALID INPUTBlueJ output of 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.) Test your program with the following data and some random data: Example 1 INPUT: N = 726 OUTPUT: 48 * 15 = 720 6 * 1 = 6 Remaining boxes = 0 Total number of boxes = 726 Total number of cartons = 16 Example 2 INPUT: N = 140 OUTPUT: 48 * 2 = 96 24 * 1 = 24 12 * 1 = 12 6 * 1 = 6 Remaining boxes = 2 * 1 = 2 Total number of boxes = 140 Total number of cartons = 6 Example 3 INPUT: N = 4296 OUTPUT: INVALID INPUT

Answered By

11 Likes