Area of Circle Java

Hey guys!! Welcome to flower brackets. Today we are going to learn area of circle java.

Area of Circle Java

 

Decades ago we have seen and learnt area of circle in our school in geometry class.

Also Read – Area of Rectangle Program in Java

I still remember the way how our geometry teacher explained the concept of area of a circle using compass and measuring the radius using ruler.

Good’ol days… Now we will see java area of a circle.

Example: area of circle java

// Area of circle is, area = pi * r * r where r is the radius of a circle 

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

public class AreaOfCircle
{
   public static void main(String[] args)
   {
      int radius = 0;
      System.out.println("Enter the radius of a Circle: ");
      // getting radius from console
      try
      {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         radius = Integer.parseInt(br.readLine());
      }
      // To check whether invalid value was entered
      catch(NumberFormatException nfe)
      {
         System.out.println("Invalid radius value" + nfe);
         System.exit(0);
      }
      catch(IOException ioe)
      {
         System.out.println("IO Error: " + ioe);
         System.exit(0);
      }
      double circleArea = Math.PI * radius * radius;
      System.out.println("Area of a circle is " + circleArea);
   }
}

Output:
Enter the radius of a Circle: 10
Area of a circle is 314.1592653589793


To calculate circumference: area of circle java

Now in area of circle in java let’s see java program to calculate area and circumference of circle.

import java.util.Scanner;

public class CircleCircumference
{
   public static void main(String[] args)
   {
      // Here we are declaring Scanner class which could read from console
      Scanner sc = new Scanner(System.in);
      double radius = 0.0;
      System.out.println("Please enter radius : ");
      radius = sc.nextDouble();
      double circumference = 0.0;
      // Formula for calculating circumference of circle
      // circumference = 2 * 22 / 7 * radius
      circumference = 2 * Math.PI * radius;    
      System.out.println("Circumference of circle is : " + circumference);
   }
}

Output:

Please enter radius : 5
Circumference of circle is : 31.41592653589793


To calculate area of circle using method

Here we are going to see java program to calculate area of circle using method.

import java.util.Scanner;

public class AreaOfCircleDemo
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);        
      System.out.println("Please enter radius : ");
      double radius = sc.nextDouble();
      double  area = calculateArea(radius);      
      System.out.println("The area of circle is : " + area);
      sc.close();
   }

   static double calculateArea(double r)
   {
      return (22 * r * r) / 7;
   }
}

Output:

Please enter radius : 10
The area of circle is : 314.2857142857143


Another way

Now let’s learn how to find circumference of circle with area. Here’s the code calculate area of circle from circumference,

public class CircleJava 
{
   public static void main(String[] args) 
   {
      int radius = 5;
      double area = Math.PI * (radius * radius);
      System.out.println("The area of a circle in java : " + area);
      double circumference = Math.PI * 2 * radius;
      System.out.println("Circumference of circle : " + circumference) ;
   }
}

Output:

The area of a circle in java : 78.53981633974483
Circumference of circle : 31.41592653589793


conclusion

So this is all about area of circle java. I hope you guys have understood java program to calculate area of circle using method.

You can subscribe to my blog flower brackets if haven’t already.

Do share this article if you like it.

You May Also Like