Computer Science, asked by shreyansh9918, 1 month ago

10. Using a switch statement , write a menu driven program to find the area and perimeter of a
square or rectangle. For finding the area and perimeter of the square input the length of the
side and for finding the area and perimeter of rectangle, input the length and breadth of the
rectangle​

Answers

Answered by kamalrajatjoshi94
8

Answer:

Program:-

import java.util.*;

public class AreaPerimeter

{

public static void main(String args[ ])

{

Scanner in=new Scanner(System.in);

int ch,pr=0,ar=0;//I declare and initialize here so as the use the same variables in the cases if I initialize in the switch statement it will give me an error

System.out.println("Enter 1 for finding the area and perimeter of square");

System.out.println("Enter 2 for finding the area and perimeter of rectangle");

System.out.println("Enter your choice");

ch=in.nextInt();

switch(ch)

{

case 1:

int l,b;

System.out.println("Enter the length and breath");

l=in.nextInt();

b=in.nextInt();

pr=2*(l+b);

ar=l*b;

System.out.println("The perimeter of rectangle="+pr);

System.out.println("The area of rectangle="+ar);

break;

case 2:

int a;

System.out.println("Enter the side of square");

a=in.nextInt();

pr=4*a;

ar=a*a;

System.out.println("The perimeter of square="+pr);

System.out.println("The area of square="+ar);

break;

default:

System.out.println("Invalid choice");

}

}

}

Similar questions