Sunday, 16 September 2012

How to calculate area, perimeter of rectangle or area, circumference of circle in Java


import java.io.*;

public class q2
{
          public static void main(String args[]) throws Exception
          {
                   InputStreamReader in = new InputStreamReader(System.in);
         
                    BufferedReader br = new BufferedReader(in);
         
                    System.out.println("1. area and perimeter of rectangle");
         
                    System.out.println("2. area and circumference of a circle");
         
                    System.out.print("Enter choice : ");
         
                    int ch=Integer.parseInt(br.readLine());
         
                    if(ch==1)
                   {
                             System.out.print("Enter rectangle length : ");
         
                             float length=Float.parseFloat(br.readLine());
         
                             System.out.print("Enter rectangle breath : ");
         
                             float breath=Float.parseFloat(br.readLine());
         
                             float rarea = length*breath;
         
                             float rperimeter = 2*(length+breath);
         
                             System.out.println("Area : "+rarea);
         
                             System.out.println("Perimeter : "+rperimeter);
                   }
                   else
                             if(ch==2)
                             {
                                      System.out.print("Enter circle radius : ");
         
                                       float crad=Float.parseFloat(br.readLine());
         
                                       float carea = 3.14f*crad*crad;
         
                                       float ccir = 2*3.14f*crad;
         
                                       System.out.println("Area : "+carea);
         
                                       System.out.println("Circumference : "+ccir);
                             }
                             else
                                      System.out.println("Wrong Input");
          }
}

No comments:

Post a Comment