Write a Java Program to find Area of Circle with example.. The area of a circle is number of square units inside the circle. Standard formula to calculate the area of a circle is: A = πr².

## Java Program to find Area of Circle using Radius

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

// Java Program to find Area of Circle using Radius package Area; 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); } }

**ANALYSIS: **

Within the following statements, **System.out.println** statement will print the statement inside the double Quotes and next we are assigning the user entered value to integer variable (radius)

System.out.println("\n Please Enter the radius of a circle: "); radius = sc.nextDouble();

Next line, we used mathematical formula to find Area and Circumference of circle

area = Math.PI * radius * radius; circumference = 2 * Math.PI * radius;

Lastly, we are displaying the output using the following statements.

System.out.println("\n Area of a Circle = " + area); System.out.println("\n Circumference of a Circle = " + circumference);

**OUTPUT**

## Java Program to find Area of Circle using Circumference

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

**JAVA CODE**

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

**ANALYSIS: **This program allows user to enter the value of a circumference and then it will calculate the area of circle as per the formula.

**OUTPUT**

## Java Program to find Area of Circle using Diameter

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

**JAVA CODE**

// Java Program to find Area of Circle using Diameter package Area; 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.fortmat("\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; } }

**OUTPUT**

**ANALYSIS **

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

**NOTE:** You can use any of the following function and both of them are mathematically correct so, you can use any of them.

When the compiler reaches to CircleArea (Diameter) line in main() program then the compiler will immediately jump to below function:

public static double CircleArea(double diameter) {

## Java Program to find Area of Circle using Oops

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

**JAVA CODE**

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, we assigned static values like 6, 30, 4 as radius, circumference and diameter to shorten the code but you can allow the user to enter any value or any business specific requirement.

**JAVA CODE**

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("\n Area of a Circle Using Radius = %.2f", area1); area2 = ac.AreaByCircumference(30); System.out.format("\n Area of a Circle Using Circumference = %.2f", area2); area3 = ac.AreaByDiameter(4); System.out.format("\n Area of a Circle Using Radius = %.2f", area3); area4 = ac.CircleAreaByDiameter(4); System.out.format("\n Area of a Circle Using Radius = %.2f", area4); } }

**OUTPUT**

**ANALYSIS**

**AreaOfACircle Class Analysis:**

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

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

First we created an instance / created an Object of the AreaOfACircle Class

AreaOfACricle ac = new AreaOfACricle();