Java Program to find Area of Circle

Write a Java Program to Find the Area of Circle with an example. The area of a circle is a 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 Area of 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 radius). In this Java program, we allow the user to enter the radius. By using that value, this Java program will find the 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);
	}
}

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 of this Java code, we used a mathematical formula to find the Area and Circumference of the 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);
Java Program to find Area of Circle using Radius

Java Program to find Area of Circle using Circumference

Distance around the circle 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 circumference and using that value we will find Area of Circle.

// 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);
	}
}

This program allows the user to enter the value of a circumference. Then it 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 Area of Circle using Diameter

The distance across the circle passing through the center, called as 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 Java program, we allow the user to enter diameter. By using that value, we will find the Area of Circle and Circumference of Circle.

// 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.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;
	}
}

Java Area of a circle output

 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 Area of 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 the 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, 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.

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);
	}
}

 Area of a Circle Using Radius = 113.10
 Area of a Circle Using Circumference = 71.62
 Area of a Circle Using Radius = 12.57
 Area of a Circle 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 above example.

Main Class Analysis:

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

AreaOfACricle ac = new AreaOfACricle();

Comments are closed.