Input
1. Radius of the circle in integer value (example radius=4)
Program Code
package programs.java4newbie.com; /* This program accepts an integer value for radius of circle and then return the area of the circle. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CircleArea { public static void main(String[] args) { float radius = 0; System.out.println("Please enter radius of a circle"); try { // get the radius from console BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); radius = Float.parseFloat(br.readLine()); } // if invalid value was entered catch (NumberFormatException nfe) { System.out .println("Invalid value for radius entered, please try again with a valid value "); System.exit(0); } catch (IOException ioe) { System.out.println("IO Error "); System.exit(0); } /* * Area of a circle is pi * r * r where r is a radius of a circle. */ // NOTE : use Math.PI constant to get value of pi double area = Math.PI * radius * radius; System.out.println("Area of a circle is " + area); } }
Output
Please enter radius of a circle 5 Area of a circle is 78.53981633974483