-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAreaOfShapes.java
More file actions
41 lines (39 loc) · 1.76 KB
/
AreaOfShapes.java
File metadata and controls
41 lines (39 loc) · 1.76 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
// 30. Write a program to calculate the area of various shapes (circle, rectangle, triangle).
import java.util.Scanner;
public class AreaOfShapes {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose a shape:");
System.out.println("1. Circle");
System.out.println("2. Rectangle");
System.out.println("3. Triangle");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter the radius of the circle: ");
double radius = scanner.nextDouble();
double circleArea = Math.PI * radius * radius;
System.out.println("Area of the circle: " + circleArea);
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
double rectangleArea = length * width;
System.out.println("Area of the rectangle: " + rectangleArea);
break;
case 3:
System.out.print("Enter the base of the triangle: ");
double base = scanner.nextDouble();
System.out.print("Enter the height of the triangle: ");
double height = scanner.nextDouble();
double triangleArea = 0.5 * base * height;
System.out.println("Area of the triangle: " + triangleArea);
break;
default:
System.out.println("Invalid choice.");
}
scanner.close();
}
}