by Dinesh Thakur

In the Following example AreaofCircleExample shows how to Calculate Area of Circle using pi*r*r. where r is a radius of a circle.

 

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

    // Declaring class name as AreaofCircleExample
    public class AreaofCircleExample {            
     
            public static void main(String[] args) {
                   
                    int r = 0;       // r for radius of circle
                    System.out.println("Please enter the radius value r : ");
                   
                    try
                    {
                            //Obtaining the keyboard input with BufferedReader
         BufferedReader Buffer = new BufferedReader(new InputStreamReader(System.in));
                            r = Integer.parseInt(Buffer.readLine());
                    }
 
                    //if radius value is invalid then Exception raised
                    catch(NumberFormatException e)
                    {
                            System.out.println("Invalid input. Not an integer " + e);
                            System.exit(0);
                    }
                    catch(IOException io)
                    {
                            System.out.println("Input Error " + io);
                            System.exit(0);
                    }
                   
                    //Math.PI = 3.14 is math constant to get value of pi
                    // Perform calculations and display result
                    // calculate the area of circle that is AreaofCircle = pi*r^2

                    double AreaofCircle = Math.PI * r * r;     
                    System.out.println("Area of a circle is " + AreaofCircle);
            }
    }
     
 

Area of Circle Example