Details of The Program: Click Here
class Pat
{
BufferedReader br=new BufferedReader (new
InputStreamReader(System.in)); int n,no,x,cartoons=0, size=48;
public void takeBoxes() throws Exception
{
System.out.print("\nEnter the number of
boxes:"); n=Integer.parseInt(br.readLine());
no=n;
if(n>=1001)
{
System.out.println("INVALID
INPUT."); return;
}
System.out.print("\nOUTPUT:");
while(true)
{
if(n/size!=0)
{
x=n/size;
System.out.print("\n"+size +
"x"+ x + "=" + (size * x)); cartoons+=n/size; n=n%size;
}
size=size/2;
if(size==0)
break;
if(n< 6 && n >0)
{
cartoons++;
}
{
System.out.print("\nRemaining
Boxes: 0"); break;
}
}
System.out.print("\nTotal No of
Boxes:" + no); System.out.print("\nTotal No of Cartoons:" +
cartoons);
}
public static void main(String args[]) throws
Exception
{
Pat ob=new Pat(); ob.takeBoxes();
}
}
Variable Description
| |||
Type
|
Name
|
Purpose
|
Scope
|
BufferedReader
|
br
|
To intake number of
|
Within the function
|
boxes
|
‘takeBoxes()’
| ||
int
|
n
|
Stores the number
|
Within the function
|
of boxes
|
‘takeBoxes()’
| ||
Int
|
no
|
Value of ‘n’ is
|
Within the function
|
assigned on it so
|
‘takeBoxes()’
| ||
that we can access
| |||
the value afterwards
| |||
Int
|
x
|
Stores the number
|
Within the function
|
of boxes of the
|
‘takeBoxes()’
| ||
specified size
| |||
Int
|
cartoons
|
Holds the number of
|
Within the function
|
cartoons used in
|
‘takeBoxes()’
| ||
storing the boxes
| |||
Int
|
size
|
Holds the size of
|
Within the function
|
cartoons
|
‘takeBoxes()’
|
Algorithm
Step 1: Create int type variables ‘n’, ‘no’, ‘x’, ‘cartoons’ and ‘size’ with 48 as initial value in size.
Create BufferedReader object ‘br’.
Step 2: Store the number of boxes in variable ‘n’ and assign the value in another variable ‘no’.
Step 3: Check the validity of the value stored in ‘n’. If invalid number of boxes is entered, exit from the program otherwise continue.
Step 4: Repeat the step nos 4 to 10 until value in size becomes 0
Step 5: Divide ‘n’ by ‘size’ and if it is not equal to 0, store the quotient in ‘x’ which represents the number of cartoons of size ‘size’.
Step 6: Display size of the cartoon, number of that sized cartoon and number of boxes in those cartoons.
Step 7: Increase the value of the variable cartoons by the number of that sized cartoons used.
Step 8: Reinitialise ‘n’ with the remaing boxes
Step 9: Reduce the value of size to half of the previous value, which represents the capacity of the next sized cartoons. If the value becomes 0, go out of step 10 other go to step 10
Step 10: if the value of ‘n’ becomes less than 6 but greater that 0 then display ‘n’ as remaining boxes and go to step 11. if the value of ‘n’ becomes 0 then display ‘n’ as remaining boxes and go to step 11. Otherwise go to step 4
Step 11: Display ‘no’ and ‘cartoons’ to show how many boxes and how many cartoons used import java.io.*;
No comments:
Post a Comment