Write a Menu driven Program to calculate the area of circle, triangle and rectangle based on the option selected by the user using switch statement. Display appropriate error message if option is below 1 or above 3. *
Answers
Question:-
Write a menu driven program to calculate the area of circle, triangle and rectangle based on the option selected by user using switch case.
Program:-
import java.util.*;
class Area
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("1. Area of Circle. ");
System.out.println("1. Area of Triangle. ");
System.out.println("1. Area of Rectangle. ");
System.out.println("Enter your choice...");
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("Enter the radius of the circle: ");
double rad=sc.nextDouble();
double a=Math.PI*rad*rad;
System.out.println("Area of the circle is: "+a);
break;
case 2:
System.out.print("Enter the length of first side: ");
double x=sc.nextDouble();
System.out.print("Enter the length of second side: ");
double y=sc.nextDouble();
System.out.print("Enter the length of third side: ");
double z=sc.nextDouble();
double s=x+y+z;
double a=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of the Triangle is: "+a);
break;
case 3:
System.out.print("Enter the length: ");
double x=sc.nextDouble();
System.out.print("Enter the breadth: ");
double y=sc.nextDouble();
System.out.println("Area is: "+x*y);
break;
default: System.out.println("An error occurred...Please try again later. ");
}
}
}