Write a program to calculate area and perimeter of a circle

In this section, you will learn to calculate the area and perimeter of a circle.

Write a program to calculate area and perimeter of a circle

In this section, you will learn to calculate the area and perimeter of a circle.

Write a program to calculate area and perimeter of a circle

Write a program to calculate area and perimeter of a circle

     

The given example will teach you the method for preparing a program to calculate the area and perimeter of a circle. First of all name a class as "CircleArea" under Java I/O package and define and integer r=o, which is the radius of the circle. Now use try exception to handle errors and other exceptional events. As we have to input the value of radius here create a buffered class with an object as 'br1'. This create a buffering character input stream that uses a default sized input buffer. The InputStreamReader here works as a translator that converts byte stream to character stream. Now type message that "Enter radius of circle" in the System.out.println method. 

Now use the parseInt() method of the Integer class in order to convert from external numeric format to internal format. Now create the Math class in which all the mathematical functions are defined. This Math class can be imported from the java.lang.* package. Write the program for both the cases: radius and perimeter. 

Before ending the program use the Catch mechanism that detects and catch user input errors. In the end compile and run the program and enter your desired value as radius for calculating the radius and perimeter of the circle.

Here is the code of the program: 

import java.io.*;
class CircleArea{
  public static void main(String[] args){
  int r=0;
 try{
  BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter Radius of Circle  : ");
  r = Integer.parseInt(br1.readLine());
  double area = java.lang.Math.PI*r*r;
  System.out.println("Area of Circle : "+area);
  double  perimeter =2*java.lang.Math.PI*r ;
  System.out.println("Perimeter of Circle : "+perimeter);
  }
  catch(Exception e){
  System.out.println("Error : "+e);
  }  
  }
}

Download this example.