Calculate Circle Area using Java Example

  1. /*
  2.         Calculate Circle Area using Java Example
  3.         This Calculate Circle Area using Java Example shows how to calculate
  4.         area of circle using it's radius.
  5. */
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. public class CalculateCircleAreaExample {
  10.         public static void main(String[] args) {
  11.                
  12.                 int radius = 0;
  13.                 System.out.println("Please enter radius of a circle");
  14.                
  15.                 try
  16.                 {
  17.                         //get the radius from console
  18.                         BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
  19.                         radius = Integer.parseInt(br.readLine());
  20.                 }
  21.                 //if invalid value was entered
  22.                 catch(NumberFormatException ne)
  23.                 {
  24.                         System.out.println("Invalid radius value" + ne);
  25.                         System.exit(0);
  26.                 }
  27.                 catch(IOException ioe)
  28.                 {
  29.                         System.out.println("IO Error :" + ioe);
  30.                         System.exit(0);
  31.                 }
  32.                
  33.                 /*
  34.                  * Area of a circle is
  35.                  * pi * r * r
  36.                  * where r is a radius of a circle.
  37.                  */
  38.                
  39.                 //NOTE : use Math.PI constant to get value of pi
  40.                 double area = Math.PI * radius * radius;
  41.                
  42.                 System.out.println("Area of a circle is " + area);
  43.         }
  44. }
  45. /*
  46. Output of Calculate Circle Area using Java Example would be
  47. Please enter radius of a circle
  48. 19
  49. Area of a circle is 1134.1149479459152
  50. */
  51. /* Please comment below for your views and if  have any problems with the above code*/

Comments