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

Write a java program to enter radius of a circle and find its area is one of the common java interview question.

java program to calculate area of a circle

In the below java program to find area of circle first user enters radius as input using nextDouble() method of Scanner class. This input is stored in double data type variable ‘radius’.

Then using area of circle formula area is calculated. Here’s the formula to find area of circle,

area = 3.142 * r * r

where r is the radius of a circle. Here’s the java program area of circle.

import java.util.Scanner;
public class CalculateAreaOfCircle
{
   public static void main(String[] args)
   {
      double radius;
      double pi = 3.142, area;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter radius of circle: ");
      radius = sc.nextDouble();
      area = pi * radius * radius;
      System.out.println("Area of circle: " + area);
      sc.close();
   }
}


Output:
Please enter radius of circle : 7
Area of circle : 153.958


Similarly let’s learn area of circle program in java without scanner. Here we have predefined value of radius of circle.

public class AreaOfCircleWithoutScanner
{
   public static void main(String[] args) 
   {
      int radius;
      double pi = 3.142, area;
      radius = 7;
      area = pi * radius * radius;
      System.out.println("Area of circle is : " + area);
   }
}


Output:

Area of circle is : 153.958


Now let’s learn area of circle program in java using inheritance. Inheritance is a procedure of acquiring all the properties and behaviour of a parent class (super class) into child class (subclass).

Inheritance represents “IS-A” relationship between super class and subclass. Here’s the java program.

import java.util.Scanner;
class CircleArea
{
   double area;
   void circle(double r)
   {
      area= (22 * r * r) / 7;
   }
}
class AreaOfCircleUsingInheritance extends CircleArea
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter radius : ");
      double radius = sc.nextDouble();      
      CircleArea cir = new CircleArea();
      cir.circle(radius);
      System.out.println("Area of Circle is: " + cir.area);
      sc.close();
   }
}


Output:

Please enter radius : 5
Area of Circle is: 78.57142857142857


Likewise let’s learn area of circle program in java using constructor. Constructor is a block of code used to initialize objects. Here’s the java program to find area of circle using constructor.

import java.util.Scanner;
class AreaCircle
{
   double area;
   AreaCircle(double radius)
   {
      area = (22 * radius * radius) / 7;
   }
}
class AreaOfCircleUsingConstructor
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter the radius : ");
      double radius = sc.nextDouble();      
      AreaCircle obj = new AreaCircle(radius);
      System.out.println("Area of Circle is: " + obj.area);
      sc.close();
   }
}


Output:

Please enter the radius : 7
Area of Circle is: 154.0


Also read – major features of java