0
0
JavaProgramBeginner · 2 min read

Java Program to Calculate Area of Circle

To calculate the area of a circle in Java, use the formula area = Math.PI * radius * radius where radius is the circle's radius; for example, double area = Math.PI * radius * radius;.
📋

Examples

Inputradius = 0
OutputArea of circle: 0.0
Inputradius = 5
OutputArea of circle: 78.53981633974483
Inputradius = 10.5
OutputArea of circle: 346.3605900582744
🧠

How to Think About It

To find the area of a circle, first get the radius from the user. Then multiply the radius by itself and by the constant value of pi. This gives the area inside the circle.
📐

Algorithm

1
Get the radius value from the user
2
Calculate area by multiplying pi with radius squared
3
Display the calculated area
💻

Code

java
import java.util.Scanner;

public class CircleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter radius of circle: ");
        double radius = scanner.nextDouble();
        double area = Math.PI * radius * radius;
        System.out.println("Area of circle: " + area);
        scanner.close();
    }
}
Output
Enter radius of circle: 5 Area of circle: 78.53981633974483
🔍

Dry Run

Let's trace the program with radius = 5 through the code

1

Input radius

User enters 5, so radius = 5.0

2

Calculate area

area = Math.PI * 5.0 * 5.0 = 3.141592653589793 * 25 = 78.53981633974483

3

Print result

Output "Area of circle: 78.53981633974483"

StepRadiusArea
Input5.0-
Calculate5.078.53981633974483
Output5.078.53981633974483
💡

Why This Works

Step 1: Getting radius input

We use Scanner to read the radius value from the user so the program can work with any circle size.

Step 2: Calculating area

The formula area = Math.PI * radius * radius calculates the space inside the circle using the radius squared and pi.

Step 3: Displaying the result

We print the area so the user can see the answer clearly.

🔄

Alternative Approaches

Using a method to calculate area
java
import java.util.Scanner;

public class CircleArea {
    public static double calculateArea(double r) {
        return Math.PI * r * r;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter radius: ");
        double radius = scanner.nextDouble();
        double area = calculateArea(radius);
        System.out.println("Area of circle: " + area);
        scanner.close();
    }
}
This approach separates calculation logic into a method, improving code reuse and clarity.
Using float instead of double
java
import java.util.Scanner;

public class CircleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter radius: ");
        float radius = scanner.nextFloat();
        float area = (float)(Math.PI * radius * radius);
        System.out.println("Area of circle: " + area);
        scanner.close();
    }
}
Using float saves memory but reduces precision, which might be okay for simple uses.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation uses a fixed number of operations regardless of input size, so it is constant time O(1).

Space Complexity

Only a few variables are used to store radius and area, so space is constant O(1).

Which Approach is Fastest?

All approaches run in constant time; using a method adds clarity but no significant speed difference.

ApproachTimeSpaceBest For
Direct calculation in mainO(1)O(1)Simple quick calculation
Separate method for calculationO(1)O(1)Code reuse and clarity
Using float instead of doubleO(1)O(1)Memory saving with less precision
💡
Always use double for precise calculations involving decimals like area.
⚠️
Beginners often forget to square the radius and just multiply by pi once.