JAVA PROGRAM 2

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 sample data and some random data:

Example 1

INPUT : N = 726

 

OUTPUT :

48 x 15 = 720

6 x 1 = 6

Remaining boxes = 0

Total number of boxes = 726

Total number of cartons = 16

 

Example 2

INPUT : N = 140

OUTPUT :

48 X 2 = 96

24 x 1 = 24

12 x 1 = 12

6 x 1 = 6

Remaining boxes 2 x 1 = 2

Total number of boxes = 140

Total number of cartons = 5

 

Example 3

INPUT : N = 4296

OUTPUT : INVALID LENGTH

 

ALGORITHM:

Step1: Start.

Step2: Declare an array c[] of size 4 to store the available sizes for packing cartons statically.

Step3: Accept number of boxes in ‘n’ to be packed from the user. Check whether it lies in range (upto 1000).

Step4:  Take a loop ranging from 0 to 4 to check for each size.

Step5: Divide n with first size (n/c[i]). The resulting quotient is number of cartons required for that size.

Step6: Check if quotient is not zero and display number of cartons which can accommodate c[i] boxes in given format.

Step7: An integer variable total is declared in which all quotients are added to find total number of cartons required.

Step8: Reduce n using modulo operator (n%c[i]) to find remaining boxes.

Step9: Exit loop. Repeat steps 5-8 till number of boxes is reduced by least available size.

Step10: Check if number of boxes left, are less than 6. Then an extra carton of capacity is used (total+1).

Step11: Display remaining boxes, number of boxes entered and total number of cartons required.

Step12: Stop.

 

Program:

import java.util.*;

class cartons

{

    Scanner sc=new Scanner(System.in);

    void main()

    {

        System.out.print("INPUT: N= ");

        int N=sc.nextInt();

        if(N>999)

        {

            System.out.println("OUTPUT:");

            System.out.println("invalid length");

        }

        else

        {

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

            int s=0;

            int a=N;

            int j=0;

            System.out.println("OUTPUT:");

            for(int i=0;i<4;i++)

            {

                int t=N/c[i];

                s=s+t;

                if(t!=0)

                {

                System.out.println(c[i]+"X"+t+"="+c[i]*t);

            }

                N=N%c[i];

                if(N<6)

                {

                    j=N;

                }

            }

            System.out.println("Remaining boxes ="+j);

            System.out.println("Total number of boxes ="+a);

            System.out.println("Total number of cartons ="+s);

        }

    }}



Sample input/output:





Random input/output:



Comments

Popular posts from this blog