Java program to calculate area of a circle

Let’s learn java program to calculate area of a circle.

java program to calculate area of a circle

Java program to calculate area of a circle

Java program to find area of circle and rectangle is one of the common java interview question.

In the below java program area of circle we are using scanner class to take input from user.

Also read – java program to calculate area of rectangle

First user enters radius as input. Then using given formula we calculate the area of a circle. Here’s the simple java program to find area of circle,

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

import java.util.Scanner;

public class AreaOfCircleDemo 
{
   public static void main(String[] args) 
   {
      double radius;
      double pi = 3.1415, area;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter radius of circle : ");
      radius = sc.nextInt();
      area = pi * radius * radius;
      System.out.println("Area of circle : " + area);
      sc.close();
   }
}



Output:
Please enter radius of circle : 5
Area of circle : 78.5

Java program to find area of circle using class and object

We can also find area of circle using class and object. To calculate area and circumference of circle using given radius first create class Circle, variable/field radius methods findArea(), findCircumference() and setValues().

Here’s the java program to find area of circle using class and object,

class Circle 
{
   double radius;
   void setValues(double rad)
   {
      radius = rad;
   }
   
   void findArea()
   {
      double circle_Area = Math.PI * radius * radius;
      System.out.println("Area of circle = " + circle_Area);
   }
    
   void findCircumference()
   {
      double circle_Circumference = 2 * Math.PI * radius ;
      System.out.println("Circumference of circle = " + circle_Circumference);
   }

   public static void main(String[] args) 
   {
      Circle obj = new Circle();
      obj.setValues(5);
      obj.findArea();
      obj.findCircumference();
   }
}


Output:

Area of circle = 78.53981633974483
Circumference of circle = 31.41592653589793

Similarly here’s the java program to find area of circle using methods – area of circle in java using method.