Cartons Java Program | ISC Computer Science 2017 Practical

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

import java.util.Scanner;
class Cartons{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("N = ");
        int n = Integer.parseInt(in.nextLine());
        if(n > 1000){
            System.out.println("INVALID INPUT");
            return;
        }
        int b6 = 0;
        int b12 = 0;
        int b24 = 0;
        int b48 = 0;
        int num = n;
        int total = 0;
        b48 = num / 48;
        if(b48 > 0)
            System.out.println("48 x " + b48 + " = " + b48 * 48);
        num %= 48;
        b24 = num / 24;
        if(b24 > 0)
            System.out.println("24 x " + b24 + " = " + b24 * 24);
        num %= 24;
        b12 = num / 12;
        if(b12 > 0)
            System.out.println("12 x " + b12 + " = " + b12 * 12);
        num %= 12;
        b6 = num / 6;
        if(b6 > 0)
            System.out.println("6 x " + b6 + " = " + b6 * 6);
        num %= 6;
        total = b48 + b24 + b12 + b6;
        if(num == 0)
            System.out.println("Remaining boxes = " + num);
        else{
            total++;
            System.out.println("Remaining boxes " + num + " x 1 = " + num);
        }
        System.out.println("Total number of boxes = " + n);
        System.out.println("Total number of cartons = " + total);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *