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
26 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



Answered By
11 Likes
Related Questions
Give the output of the following program segment. How many times is the loop executed?
for(x=10; x>20;x++) System.out.println(x); System.out.println(x*2);
Define a class to accept a number from user and check if it is an EvenPal number or not.
(The number is said to be EvenPal number when number is palindrome number (a number is palindrome if it is equal to its reverse) and sum of its digits is an even number.)
Example: 121 – is a palindrome number
Sum of the digits – 1+2+1 = 4 which is an even numberDefine a class to accept a four digit number and check whether it is a Framul Number or not. A number is called a Framul Number if the product of all its digits equals fives times the sum of its first and last digit.
Example 1:
Input: 1325
Product of all digits = 1 x 3 x 2 x 5 = 30
Five times sum of first & last digit = 5 x (5 + 1) = 5 x 6 = 30Output: Framul Number
Example 2:
Input: 2981
Product of all digits = 2 x 9 x 8 x 1 = 144
Five times sum of first & last digit = 5 x (1 + 2) = 5 x 3 = 15Output: Not a Framul Number
If the entered number is not of four digits, the message "Please enter a four digit number" should be shown.
import java.util.Scanner; public class KboatFramulNumber { public static void main(String args[]) { Scanner sc = _____(1)_____ System.out.print("Enter a four-digit number: "); int num = _____(2)_____ _______(3)_______ { System.out.println("Please enter a four-digit number"); return; } int d = 0; int f = num % 10; int p = 1; _______(4)_______ { _____(5)_____ _____(6)_____ _____(7)_____ } int s = _____(8)_____ if (p == s) { _______(9)_______ } else { _______(10)_______ } } }
Define a class to accept a number and check whether it is an FDS Number or not. A number is called an FDS Number if the sum of the factorials of its digits equals the number itself.
Example 1:
Input: 145
Output: FDS Number [1! + 4! + 5! = 1 + 24 + 120 = 145]Example 2:
Input: 123
Output: Not an FDS Number [1! + 2! + 3! = 1 + 2 + 6 ≠ 123]import java.util.Scanner; class KboatFDSNum { static int fact(int d) { int f = 1; _______(1)_________ { _______(2)_________ } _______(3)_________ } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); int num = in.nextInt(); int t = num, sum = 0; _______(4)_________ { _______(5)_________ _______(6)_________ _______(7)_________ } _______(8)_________ { _______(9)_________ } else { _______(10)_________ } } }