Computer Science, asked by UtkarshChawhan, 3 months ago

Write a menu driven Java program to calculate the Area of Square, Rectangle and Circle
according to user’s choice.
Hint: Area of Square – Side 2
Area of Rectangle – Length x Breadth
Area of circle – (22/7) x r2

Answers

Answered by anindyaadhikari13
23

Required Answer:-

Question:

  • Write a menu driven program in Java to calculate area of square, rectangle, circle as per user's choice.

Solution:

Here is the code.

import java.util.*;

public class SwitchCase {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("1. Area of Square.");

System.out.println("2. Area of a Rectangle.");

System.out.println("3. Area of a circle.");

System.out.print("\nEnter your choice: ");

switch(sc.nextInt())

{

case 1:

System.out.print("Enter side length: ");

double s=sc.nextDouble();

s*=s;

System.out.println("Area of the Square is: "+s);

break;

case 2:

System.out.print("Enter length of rectangle: ");

double l=sc.nextDouble();

System.out.print("Enter breadth of rectangle: ");

double b=sc.nextDouble();

System.out.println("Area of the rectangle is: "+l*b);

break;

case 3:

System.out.print("Enter radius of the circle: ");

double rad=sc.nextDouble();

double ar=Math.PI*rad*rad;

System.out.println("Area of the circle is: "+ar);

break;

default: System.out.println("Invalid Choice.");

}

sc.close();

}

}

Explanation:

  • At first, we will import Scanner class. It is used to take input from Keyboard. Then, we will ask the user to enter his/her choice. If the choice is 1, then it will take side as input and calculate the area of the square. If the choice is 2, then it will calculate the area of rectangle taking length and breadth as input. If the choice is 3, it will calculate the area of the circle. Note that Math.PI is constant. It stores the value of pi upto few decimal places. If the choice given are other that 1,2 or 3, then a message will be displayed, - "Invalid Choice"

Output is attached.

Attachments:
Answered by Oreki
6

Code:

import java.util.Scanner;

public class Areas {

   public static void main(String[] args) {

       Scanner sc = new Scanner(System.in);

       System.out.println("Enter - \n" +

                          "  1. Square \n" +

                          "  2. Rectangle \n" +

                          "  3. Circle");

       switch (sc.nextInt()) {

           case 1:

               System.out.print("Enter the Side of the Square - ");

               float side = sc.nextFloat();

               System.out.println("Area - " + (side * side) + " unit square");

               break;

           case 2:

               System.out.print("Enter Length of the Rectangle - ");

               float length = sc.nextFloat();

               System.out.print("Enter Breadth of the Rectangle - ");

               float breadth = sc.nextFloat();

               System.out.println("Area - " + (length * breadth) + " unit square");

               break;

           case 3:

               System.out.print("Enter the Radius of the Circle - ");

               float radius = sc.nextFloat();

               System.out.println("Area - " + (Math.PI * radius * radius) + " unit square");

               break;

           default:

               System.out.println("Invalid Choice!");

       }

   }

}

Sample I/O:

   Enter -  

     1. Square  

     2. Rectangle  

     3. Circle

   Enter Choice - 2

   Enter Length of the Rectangle - 12

   Enter Breadth of the Rectangle - 23

   Area - 276.0 unit square

Similar questions