Turn Desktop View Off
by Dinesh Thakur Category: Data Type

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 : ");
                            //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);
                    catch(IOException io)
                            System.out.println("Input Error " + io);
                    //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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.