A company manufactures packing cartons in four sizes, i.e. cartons to accomodate 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.io.*;
class Cartons{
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("N = ");
int n = Integer.parseInt(br.readLine());
if(n > 1000){
System.out.println("INVALID INPUT");
return;
}
int num = n;
int c48 = n / 48;
if(c48 > 0)
System.out.println("48 x " + c48 + " = " + (c48 * 48));
n %= 48;
int c24 = n / 24;
if(c24 > 0)
System.out.println("24 x " + c24 + " = " + (c24 * 24));
n %= 24;
int c12 = n / 12;
if(c12 > 0)
System.out.println("12 x " + c12 + " = " + (c12 * 12));
n %= 12;
int c6 = n / 6;
if(c6 > 0)
System.out.println("6 x " + c6 + " = " + (c6 * 6));
n %= 6;
if(n == 0)
System.out.println("Remaining boxes = 0");
else
System.out.println("Remaining boxes = " + n + " x 1 = " + n);
System.out.println("Total number of boxes = " + num);
int total = c48 + c24 + c12 + c6;
if(n > 0)
total++;
System.out.println("Total number of cartons = " + total);
}
}
```

## Algorithm for the above program

1. Input N

2. If N > 1000 then print “Invalid Input” and Exit.

3. NUM = N

4. C48 = N / 48

5. If C48 > 0 then print C48 * 48

6. N = N MOD 48

7. C24 = N / 24

8. IF C24 > 0 then print C24 * 24

9. N = N MOD 24

10. C12 = N / 12

11. IF C12 > 0 then print C12 * 12

12. N = N MOD 12

13. C6 = N / 6

14. IF C6 > 0 then print C6 * 6

15. N = N % 6

16. If N = 0 then print “Remaining boxes = 0” Else print “Remaining boxes = ” + N

17. Print “Total number of boxes = ” + NUM

18. Total = C48 + C24 + C12 + C6

19. If N > 0 then Total = Total + 1

20. Print “Total number of cartons = ” + Total

21. Stop

## 4 replies on “Cartons Java Program ISC 2017 Practical”

In Example 2, the number of cartons is actually 5 and not 6. Please make the required changes. Thank You.

6 is correct because 1 extra carton is required to store remaining 2 boxes.

Can i get the algorithm for this?

I have posted the algorithm for the mentioned program in the same page. Thank you.