Write a program to accept the choice [Rectangle/Square/Triangle] from the user and calculate the area and perimeter for the same. Display a menu for the user in the format given below:
Answers
Answer:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
System.out.println("Which polygon's perimeter and area do you want to find?");
System.out.println("your choice are \n 1)square \n 2)rectangle \n 3)triangle");
Scanner sc = new Scanner(System.in);
String choice = sc.nextLine();
if(choice.equalsIgnoreCase("square")){
System.out.print("Enter the length of each side of the square:");
int l = sc.nextInt();
int ar = l * l;
int per = l * 4;
System.out.println("The perimter of the square is " + per);
System.out.println("The area of the square is " + ar);
}
else if(choice.equalsIgnoreCase("rectangle")){
System.out.print("Enter the length:");
int l1 = sc.nextInt();
System.out.print("Enter the breadth:");
int b = sc.nextInt();
int per1 = 2 * (l1 + b);
int ar1 = l1 * b;
System.out.println("The perimeter of the rectangle is " + per1);
System.out.println("The area of the rectangle is " + ar1);
}
else if(choice.equalsIgnoreCase("triangle")){
System.out.print("Enter the length of the base of the triangle:");
int base = sc.nextInt();
System.out.print("Enter the length of the height of the triangle:");
int h = sc.nextInt();
System.out.print("Enter the length of each side:");
int side = sc.nextInt();
int ar2 = (base + h) / 2;
int per2 = side * 3;
System.out.println("The perimeter of the triangle is " + per2);
System.out.println("The area of the triangle is " + ar2);
}
else{
System.out.println("your choice are \n 1)square \n 2)rectangle \n 3)triangle");
}
}
}
Explanation: