Write a Java Program to Find the Area of a Circle with an example. The area of a circle is the total number of square units inside the circle. The standard formula to calculate the area of a circle is A = πr².

## Java Program to find the Area of a Circle using Radius

If we know the radius, we can calculate the area of a circle using the formula: A = πr² (Here, A is the area of a circle, and r is the radius). In this Java program, we allow the user to enter the radius. By using that value, this Java program will find the Area and Circumference of the Circle.

Within the first statements, the System.out.println statement will print the statement inside the double Quotes. And next, we assign the user entered value to an integer variable (radius)

Next line of this Java program code, we used a mathematical formula to find the Area and Circumference of the circle.

Lastly, we display the output using the Java System.out.println statements.

import java.util.Scanner; public class AreaOfCircle { private static Scanner sc; public static void main(String[] args) { double radius, area, circumference; sc = new Scanner(System.in); System.out.println("\n Please Enter the radius of a circle: "); radius = sc.nextDouble(); area = Math.PI * radius * radius; circumference = 2 * Math.PI * radius; System.out.println("\n Area of a Circle = " + area); System.out.println("\n Circumference of a Circle = " + circumference); } }

## Java Program to find the Area of a Circle using the Circumference

The distance around the circle is called the circumference. If you know the circumference, we can calculate the area of a circle using the formula: A= C²⁄ 4π (Here, C is circumference). In this Java program, we allow the user to enter the circumference, and using that value, we will find the Area of the Circle.

import java.util.Scanner; public class AreaOfCircleUsingCircum { private static Scanner sc; public static void main(String[] args) { double ar, cir; sc = new Scanner(System.in); System.out.println("\n Please Enter the Circumference of a circle: "); cir = sc.nextDouble(); ar = (cir * cir) /(4* Math.PI); System.out.println("\n Area of a Circle = " + ar); } }

This program allows the user to enter the value of a circumference. The program will calculate the area of a circle as per the formula.

```
Please Enter the Circumference of a circle:
34
Area of a Circle = 91.99155710711551
```

## Java Program to find the Area of a Circle using the Diameter

The distance across the circle passes through the center, called diameter. If we know the diameter, we can calculate the area of a circle using the formula: A=π/4*D² (D is the diameter). In this program, we allow the user to enter the diameter. By using that value, we will find the Area and Circumference of the Circle.

import java.util.Scanner; public class AreaOfCircleUsingMethods { private static Scanner sc; public static void main(String[] args) { double diameter, area1, area2; sc = new Scanner(System.in); System.out.println("\n Please Enter the Diameter of a circle: "); diameter = sc.nextDouble(); area1 = CircleArea(diameter); System.out.format("\n First Method: Area of a Circle = %.2f", area1); area2 = AreaCircle(diameter); System.out.format("\n Second Method: Area of a Circle = %.2f", area2); } public static double CircleArea(double diameter) { double area; area = (Math.PI / 4)* (diameter * diameter); return area; } public static double AreaCircle(double diameter) { double radius, area; //diameter = 2 * radius radius = diameter/2; area = Math.PI * radius * radius; return area; } }

```
Please Enter the Diameter of a circle:
3
First Method: Area of a Circle = 7.07
Second Method: Area of a Circle = 7.07
```

This program allows the user to enter the value of the diameter. Next, it will calculate the area of a circle as per the above formula. If you observe the above code, we defined two static functions of double type.

NOTE: You can use any of the following functions, and both of them are mathematically correct. So, you can use any of them.

When the compiler reaches to CircleArea (Diameter) line in the main() program, the compiler jumps immediately to the below function:

public static double CircleArea(double diameter) {

## Java Program to find the Area of a Circle using Oops

In this Java program, we will find the area of a circle using Circumference, Diameter, and Radius. In this example, we are dividing the code using Object Oriented Programming. To do this, First, we create a class that holds methods.

package Area; public class AreaOfACricle { double radius, area; public double AreaByRadius(double radius) { area = Math.PI * radius * radius; return area; } public double AreaByCircumference(double circumference) { area = (circumference * circumference) /(4* Math.PI); return area; } public double AreaByDiameter(double diameter) { area = (Math.PI / 4)* (diameter * diameter); return area; } public double CircleAreaByDiameter(double diameter) { radius = diameter/2; area = Math.PI * radius * radius; return area; } }

Within the Main program, we will create an instance of the above-specified class and call the methods. Remember, to shorten the code, and we assigned static values like 6, 30, and 4 as radius, circumference, and diameter. But you can allow the user to enter any value or business-specific requirement.

package Area; public class AreaOfCricleUsingClass { public static void main(String[] args) { double area1, area2, area3, area4; AreaOfACricle ac = new AreaOfACricle(); area1 = ac.AreaByRadius(6); System.out.format("\nUsing Radius = %.2f", area1); area2 = ac.AreaByCircumference(30); System.out.format("\nUsing Circumference = %.2f", area2); area3 = ac.AreaByDiameter(4); System.out.format("\nUsing Radius = %.2f", area3); area4 = ac.CircleAreaByDiameter(4); System.out.format("\nUsing Radius = %.2f", area4); } }

```
Using Radius = 113.10
Using Circumference = 71.62
Using Radius = 12.57
Using Radius = 12.57
```

AreaOfACircle Class Analysis:

Here we declared 4 functions of double type, and each one accepts one argument. We already explained the Logic in the above example.

Main Class Analysis:

First, we created an instance or Object of the AreaOfACircle Class

AreaOfACricle ac = new AreaOfACricle();

Comments are closed.