Write a program to display a menu to calculate area of square, rectangle and circle
Calculate the area based on the user's choice.
java program
Answers
Answer:
Program:-
import java.util.*;
public class Brainly_Answer
{
public static void main(String args[ ])
{
Scanner in=new Scanner(System.in);
int ch,ar=0;
System.out.println("Enter 1 to calculate the area of square");
System.out.println("Enter 2 to calculate the area of rectangle");
System.out.println("Enter 3 to calculate the area of circle");
System.out.println("Enter your choice");
ch=in.nextInt();
switch(ch)
{
case 1:
int a;
System.out.println("Enter the side of square");
a=in.nextInt();
ar=a*a;
System.out.println("The area of square="+ar);
break;
case 2:
int l,b;
System.out.println("Enter the length and breath of rectangle");
l=in.nextInt();
b=in.nextInt();
ar=l*b;
System.out.println("The area of rectangle="+ar);
break;
case 3:
int r;
double area=0;
System.out.println("Enter the radius of circle");
r=in.nextInt();
area=22.0/7.0*r*r;
System.out.println("The area of circle="+area);
break;
default:
System.out.println("Invalid choice");
}
}
}
Understand the program:-
- I used ar variable so that I can use it multiple times for circle I used double area as the answer is always in double data type.
- I entered 1 for choice and entered side of square 6 it gave the result 36
- If I entered 2 for choice and entered side of rectangle 7 and 9 it gave me output 63
- If I entered 3 for choice and entered radius as 7 cm so it gave me result as 154
Sorry I didn't gave the input in the photos as the limit will be crossed so I told u the input here.