Let’s learn java program to calculate area of a circle.
Java program to calculate area of a circle
To calculate 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 calculate area of circle,
area = 3.142 * r * r
where r is the radius of a circle. Here’s the program to calculate 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 to calculate area of circle without using Scanner. Here we have predefined the value of radius.
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 to calculate area of circle using inheritance. Inheritance is a procedure of acquiring all the properties and behaviour of a parent class (super class) into child class (sub class).
Inheritance represents “IS-A” relationship between super class and sub class. 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 to calculate area of circle using constructor. Constructor is a block of code used to initialize objects. Here’s the program to calculate 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