Answer:
import java.util.Scanner;
public class area_of_cir_squr_rect_tri_using_switch {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("MENU:");
System.out.println("1.Area of circle");
System.out.println("2.Area of square");
System.out.println("3.Area of rectangle");
System.out.println("4.Area of triangle");
System.out.println("Please enter any of the above option: ");
int num = sc.nextInt();
switch(num)
case 1:
System.out.println("Please enter radius of circle: ");
double radius = sc.nextFloat();
double areaCircle = (22 * radius * radius) / 7;
System.out.println("Area of circle is: " + areaCircle);
break;
case 2:
System.out.println("Please enter size of square: ");
int size = sc.nextInt();
int areasquare = size * size;
System.out.println("Area of ractangle is: " + areasquare);
case 3:
System.out.println("Please enter length and breadth of rectangle: ");
int length = sc.nextInt();
int breadth = sc.nextInt();
int areaRectangle = length * breadth;
System.out.println("Area of ractangle is: " + areaRectangle);
case 4:
System.out.println("Please enter base and height of triangle: ");
double base = sc.nextFloat();
double height = sc.nextFloat();
double areaTriangle = (base* height) / 2;
System.out.println("Area of triangle is: " + areaTriangle);
default:System.exit(0);
}
sc.close();
System.out.println("Welcome to computerpathsala.com");
Copyright © 2024 EHUB.TIPS team's - All rights reserved.
Answers & Comments
Answer:
Sure! Here's an example of a program that calculates the area of a square, triangle, or rectangle based on the user's choice using a switch case:
```python
def calculate_area():
choice = input("Enter your choice (1 for square, 2 for triangle, 3 for rectangle): ")
# Convert the choice to an integer
choice = int(choice)
# Switch case
if choice == 1:
side = float(input("Enter the side length of the square: "))
area = side * side
print("The area of the square is:", area)
elif choice == 2:
base = float(input("Enter the base length of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = 0.5 * base * height
print("The area of the triangle is:", area)
elif choice == 3:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = length * width
print("The area of the rectangle is:", area)
else:
print("Invalid choice!")
# Call the function
calculate_area()
```
You can run this program and choose the desired shape to calculate its area. Let me know if you have any questions!
Verified answer
import java.util.Scanner;
public class area_of_cir_squr_rect_tri_using_switch {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("MENU:");
System.out.println("1.Area of circle");
System.out.println("2.Area of square");
System.out.println("3.Area of rectangle");
System.out.println("4.Area of triangle");
System.out.println("Please enter any of the above option: ");
int num = sc.nextInt();
switch(num)
{
case 1:
System.out.println("Please enter radius of circle: ");
double radius = sc.nextFloat();
double areaCircle = (22 * radius * radius) / 7;
System.out.println("Area of circle is: " + areaCircle);
break;
case 2:
System.out.println("Please enter size of square: ");
int size = sc.nextInt();
int areasquare = size * size;
System.out.println("Area of ractangle is: " + areasquare);
break;
case 3:
System.out.println("Please enter length and breadth of rectangle: ");
int length = sc.nextInt();
int breadth = sc.nextInt();
int areaRectangle = length * breadth;
System.out.println("Area of ractangle is: " + areaRectangle);
break;
case 4:
System.out.println("Please enter base and height of triangle: ");
double base = sc.nextFloat();
double height = sc.nextFloat();
double areaTriangle = (base* height) / 2;
System.out.println("Area of triangle is: " + areaTriangle);
break;
default:System.exit(0);
}
sc.close();
System.out.println("Welcome to computerpathsala.com");
}
}