Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to Calculate Area and Circumference of Circle in a Java Program

In this post, I will teach you How to calculate Area of Circle and Circumference / Perimeter of Circle ? through Java source code. I assume you know how to write and understand simple Java program. For calculating Area and Circumference of Circle, you should know their formulas.

  • Area of Circle = PI * R* R , where PI = 22/7, R= Radius of given Circle.
  • Circumference of Circle = 2 * PI * R , where PI=22/7, R=Radius of given Circle.


Java program: To Calculate Area and Circumference of Circle

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Circle {
    public static void main(String[ ] args){
        int radius = 0;
        System.out.println("Enter radius of a circle : ");
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            radius = Integer.parseInt(br.readLine());
        }
        catch(NumberFormatException ne)
        {
            System.out.println("Invalid radius value" + ne);
            System.exit(0);
        }
        catch(IOException ioe)
        {
            System.out.println("IO Error :" + ioe);
            System.exit(0);
        }
        radius = 5;
        double perimeter = 2 * Math.PI * radius;
        double area = Math.PI * radius * radius;
        
        System.out.println("Area of a circle is " + area);
        System.out.println("Perimeter of a circle is " + perimeter);

        }
}  
Output of the program on Console window Enter radius of a circle : 7 Area of a circle is 78.53981633974483 Perimeter of a circle is 31.41592653589793 A Closer Look at the Sample Program Line 1 to 3 :- If we look at the source code of the program, the program starts with a import statements. Generally we have to import the classes which we require in our source code. These classes which we import are helper classes. Without importing these classes if you use them in your program, you get compile time errors. In this sample program we import three classes. Line 5 to 8 :- After importing the three classes, we create a Java class with a name Circle. In Circle.java class we have a main method which is the starting point for the execution of code. In main method we create a integer variable radius and assign it a value 0. Generally we will need this variable to store the radius provided by the user.Then we wrote a System.out.println(""); code, this prints String asking user to enter value of radius. Line 9 to 23 :- Moving ahead we wrote a try-catch statements which helps us in catching errors and issues. If we look into code two exception are been caught, i.e. NumberFormatException and IOException. If we enter an invalid integer then NumberFormatException occurs and if there is any errors in transferring of Strings from console then IOException occurs. BufferedReader class is used which creates buffer to store characters from InputStreamReader. The InputStreamReader converts byte stream to character stream. Next line we use parseInt() method of the Integer class, because this method has capability to convert numeric format to internal format. Now we use Math class which stores value for PI. Math class contains all defined mathematical functions and it is present in the package java.lang.*; Line 25 to 29 : - At last few lines of code Area and Perimeter is being calculated using the formulas stated above. Thus the final result is then printed.
 
© 2021 Learn Java by Examples Template by Hubberspot