# 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 where user enters radius of a circle and calculate its area is one of the common java interview question.

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 pi = 3.142, area;
Scanner sc = new Scanner(System.in);
System.out.println("Area of circle: " + area);
sc.close();
}
}
```

Output:
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)
{
double pi = 3.142, area;
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);
CircleArea cir = new CircleArea();
System.out.println("Area of Circle is: " + cir.area);
sc.close();
}
}
```

Output:

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;
{
}
}
class AreaOfCircleUsingConstructor
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);