KnowledgeBoat Logo

Computer Applications

Write a menu driven program to calculate:

  1. Area of a circle = p*r2, where p = (22/7)
  2. Area of a square = side*side
  3. Area of a rectangle = length*breadth

Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle.

Java

Java Conditional Stmts

220 Likes

Answer

import java.util.Scanner;

public class KboatMenuArea
{
    public static void main(String args[]) {
        
        Scanner in = new Scanner(System.in);
        
        System.out.println("Enter c to calculate area of circle");
        System.out.println("Enter s to calculate area of square");
        System.out.println("Enter r to calculate area of rectangle");
        System.out.print("Enter your choice: ");
        char choice = in.next().charAt(0);
        
        switch(choice) {
            case 'c':
                System.out.print("Enter radius of circle: ");
                double r = in.nextDouble();
                double ca = (22 / 7.0) * r * r;
                System.out.println("Area of circle = " + ca);
                break;
                
            case 's':
                System.out.print("Enter side of square: ");
                double side = in.nextDouble();
                double sa = side * side;
                System.out.println("Area of square = " + sa);
                break;
                
            case 'r':
                System.out.print("Enter length of rectangle: ");
                double l = in.nextDouble();
                System.out.print("Enter breadth of rectangle: ");
                double b = in.nextDouble();
                double ra = l * b;
                System.out.println("Area of rectangle = " + ra);
                break;
                
            default:
                System.out.println("Wrong choice!");
        }
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a menu driven program to calculate: (a) Area of a circle = p*r 2 , where p = (22/7) (b) Area of a square = side*side (c) Area of a rectangle = length*breadth Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle.BlueJ output of Write a menu driven program to calculate: (a) Area of a circle = p*r 2 , where p = (22/7) (b) Area of a square = side*side (c) Area of a rectangle = length*breadth Enter 'c' to calculate area of circle, 's' to calculate area of square and 'r' to calculate area of rectangle.

Answered By

76 Likes


Related Questions