(Compute the volume of a cylinder) Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas:
area = radius * radius * π
volume = area * length
Solution
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] args)
{
// Create a Scanner object
Scanner input = new Scanner(System.in);
final double PI = 3.14159265359;
// Prompt user to enter the radius and length of a cylinder
System.out.print("Enter the radius and length of a cylinder: ");
double radius = input.nextDouble();
double length = input.nextDouble();
// Calculate the area and volume
double area = radius * radius * PI;
double volume = area * length;
// Display results
System.out.println("The area is " + area);
System.out.println("The volume is " + volume);
}
}
OUTPUT
Enter the radius and length of a cylinder: 12 13
The area is 452.38934211696
The volume is 5881.06144752048
If you have any doubts or questions, please let me know.