Suppose that you work for a beverage company. The company wants to know the opti
ID: 3776162 • Letter: S
Question
Suppose that you work for a beverage company. The company wants to know the optimal cost for a cylindrical container that holds a specified volume. Write a program that uses an ask-before-iterating loop. During each iteration of the loop, your code will ask the user to enter the volume and the radius of the cylinder. Compute and display the height and cost of the container. Use the following formulas, where V is the volume, r is the radius, h is the height, and C is the cost.
Use 3 digits number and 2 or 3 decimal points in the result/output because they talking about the price/money so it should be 3 digits number only... example: 175.50 or 175.050
V C = 2r(r + h)Explanation / Answer
program:
import java.utill.scanner;
Class Volume
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter cylinder volume or -1 to exit:");
double volume = in.nextDouble();
double radius = 0.0;
if(volume > 0.0)
{
System.out.println("Enter cylinder radius:");
radius = in.nextDouble();
}
while(volume > 0.0)
{
double height = volume / (Math.PI * (radius * radius));
double cost = 2 * Math.PI * radius* (radius + height);
System.out.println("Height: " + h + " Cost: " + c);
System.out.println("Enter cylinder volume or -1 to exit:");
volume = in.nextDouble();
if(volume > 0.0)
{
System.out.println("Enter cylinder radius:");
radius = in.nextDouble();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.