-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolumeofCylinder.java
29 lines (22 loc) · 1.15 KB
/
VolumeofCylinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Importing the Scanner class to take user input
import java.util.Scanner;
class VolumeofCylinder {
public static void main(String[] args) {
// Creating a Scanner object to read input from the user
Scanner sc = new Scanner(System.in);
// Declaring variables for radius, height, and volume of the cylinder
double radius;
double height;
double volumeofCylinder;
// Prompting the user to enter the radius of the cylinder
System.out.println("Enter the radius of the cylinder:");
radius = sc.nextDouble(); // Reading radius input from user
// Prompting the user to enter the height of the cylinder
System.out.println("Enter the height of the cylinder:");
height = sc.nextDouble(); // Reading height input from user
// Calculating the volume of the cylinder using the formula: π * r^2 * h
volumeofCylinder = Math.PI * radius * radius * height;
// Displaying the calculated volume with proper spacing
System.out.println("Volume of the cylinder with radius " + radius + " and height " + height + " is " + volumeofCylinder);
}
}