-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolume.java
More file actions
90 lines (74 loc) · 3.4 KB
/
Copy pathVolume.java
File metadata and controls
90 lines (74 loc) · 3.4 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Volume of Cylinder, Cone, Sphere, Prism, Pyramid
import java.util.*;
public class Volume {
public static void main(String[] args) {
// Scanner object to take input from standard input stream
Scanner scanner = new Scanner(System.in);
// Taking input of choice from user
System.out.println("Enter your choice 1.Cylinder; 2.Cone; 3.Sphere; 4.Prism; 5.Pyramid:");
int choice = scanner.nextInt();
// Variable to store volume
double volume;
if (choice == 1) {
// For cylinder: Taking input
System.out.println("Enter radius");
double radius = scanner.nextDouble();
System.out.println("Enter height");
double height = scanner.nextDouble();
// Logic for volume of cylinder
// Volume = pi * (radius ^ 2) * height
volume = Math.PI * Math.pow(radius, 2) * height;
// Printing result
System.out.println("Volume of cylinder is: " + volume + " cb.unit");
} else if (choice == 2) {
// For cone: Taking input
System.out.println("Enter radius");
double radius = scanner.nextDouble();
System.out.println("Enter height");
double height = scanner.nextDouble();
// Logic for volume of cone
// Volume = (1 / 3) * pi * (radius ^ 2) * height
volume = (1.0 / 3 * Math.PI * Math.pow(radius, 2) * height);
// Printing result
System.out.println("Volume of cone is: " + volume + " cb.unit");
} else if (choice == 3) {
// For sphere: Taking input
System.out.println("Enter radius");
double radius = scanner.nextDouble();
// Logic for volume of sphere
// Volume = (4 / 3) * pi * (radius ^ 3)
volume = (4.0 / 3) * Math.PI * Math.pow(radius, 3);
// Printing result
System.out.println("Volume of sphere is: " + volume + " cb.unit");
} else if (choice == 4) {
// For prism: Taking input
System.out.println("Enter apothem length");
double apothemLength = scanner.nextDouble();
System.out.println("Enter base length");
double baseLength = scanner.nextDouble();
System.out.println("Enter height of prism");
double height = scanner.nextDouble();
// Logic for volume of prism
// Volume = a * b * h
// Where: a-Apothem length of a triangular prism
// b-Base length of a triangular prism
// h-height of a triangular prism
volume = apothemLength * baseLength * height;
// Printing result
System.out.println("Volume of prism is: " + volume + " cb.unit");
} else if (choice == 5) {
// For pyramid: Taking input
System.out.println("Enter side length");
double side = scanner.nextDouble();
System.out.println("Enter height");
double height = scanner.nextDouble();
// Logic for volume of pyramid
// Volume = 1 / 3 * base area * height
volume = (1.0 / 3) * Math.pow(side, 2) * height;
// Printing result
System.out.println("Volume of pyramid is: " + volume + " cb.unit");
} else {
System.out.println("Enter valid choice! Try Again.");
}
}
}