Write a program using a function called area() to compute area of the following

(a) Area of circle = 22/7*r*r

(b) Area of square= side * side

(c) Area of rectangle = length * breadth

Display the menu to display the area as per the user's choice.


import java.util.*;
class Area_Calc
{
  void area()
  {
     double ar=0.0,s=0.0,b=0.0,l=0.0,r=0.0;
     int ch;
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter 1 for Area of circle");
     System.out.println("Enter 2 for Area of square");
     System.out.println("Enter 3 for Area of rectangle");
     System.out.println("Enter your choice");
     ch=sc.nextInt();
     switch(ch)
     {
        case 1:
          System.out.println("Enter the radius of circle");
          r=sc.nextDouble();
          ar=(22.0/7.0)*r*r;
          System.out.println("Area of circle:"+ar);
          break;
        case 2:
          System.out.println("Enter the side of square");
          s=sc.nextDouble();
          ar=s*s;
          System.out.println("Area of square:"+ar);
          break;
        case 3:
          System.out.println("Enter the length & breadth of rectangle");
          l=sc.nextDouble();
          b=sc.nextDouble();
          ar=l*b;
          System.out.println("Area of rectangle:"+ar);
          break;
        default:
          System.out.println("You have entered a wrong choice");
        }
    }
    public static void main(String args[])
    {
        Area_Calc obj=new Area_Calc();
        obj.area();
    }
}

No comments

Powered by Blogger.