Home Java Beginners Write a program to calculate area and perimeter of a circle
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Write a program to calculate area and perimeter of a circle

Advertisement
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

     

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.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Ask Questions?    Discuss: Write a program to calculate area and perimeter of a circle   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
kiruba
June 26, 2011
simple areona of circule program

Hi Friends, How to creat simple program in java area of circule and without constructor Regards, kiruba. Mob:9445147245
prashant bhute
November 18, 2011
perforfom the c lagvege

for the matrix
Quaye-Adio Charles
November 29, 2011
computer science

needs more code to study and site is very good and it will be helpful to anyone who want to learn programing.
liya
December 21, 2011
program for area and perimeter

i dont understend the use of two lines: "double area = java.lang.Math.PI*r*r;" "double perimeter =2*java.lang.Math.PI*r ;" please explain it.
Muhammad arifin tsanie
January 11, 2012
input data from keyboard

can u tell me whats different input data from keyboard between jOptionPane version and BufferReader version? thanks for ur answer..
DMCA.com