ThisCalculator

Everyday

Java Program to Calculate Area of Circle

Updated · Standard formula, shown below

Area 78.54 cm²
Radius
5 cm
You entered

This article explains how to write a Java program that calculates the area of a circle. You provide the radius (or diameter) as input, and the program outputs the area using the formula A = πr². The code examples below cover console, method-based, and object-oriented approaches.

Formula

The area of a circle is given by the standard geometric formula:

A = π × r²

Where:

  • A = area of the circle (in square units, e.g., cm², m², in²)
  • π (pi) ≈ 3.141592653589793 (the ratio of a circle’s circumference to its diameter)
  • r = radius of the circle (half the diameter, in the same length unit)

If you only know the diameter d, the formula becomes A = π × (d/2)² = (π/4) × d².

Worked Example

Using the default radius of 5 cm from the calculator above:

  1. Start with the formula: A = π × r²
  2. Plug in r = 5 cm: A = π × 5²
  3. Square the radius: 5² = 25
  4. Multiply by π: A = 3.14159 × 25 = 78.53975…
  5. Round to two decimal places: 78.54 cm²

This matches the calculator’s output exactly.

Basic Java Program (Console)

The simplest way is to read the radius from the user and print the area. Here’s a complete program using Scanner:

import java.util.Scanner;

public class CircleArea {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter radius: ");
        double r = input.nextDouble();
        double area = Math.PI * r * r;
        System.out.printf("Area = %.2f%n", area);
        input.close();
    }
}

This code uses Math.PI (a built-in constant) and Math.pow(r, 2) if you prefer Math.pow instead of r*r. The printf with %.2f rounds the output to two decimal places.

Using a Method (Reusable Logic)

For larger programs, you typically wrap the calculation in a method so it can be called multiple times:

public static double circleArea(double radius) {
    return Math.PI * radius * radius;
}

// Call it:
double area = circleArea(5.0); // returns 78.53981633974483

This approach keeps the calculation separate from the input/output logic, making it easier to test and reuse.

Object-Oriented Version

If you’re learning Java, you might want a Circle class:

public class Circle {
    private double radius;

    public Circle(double r) {
        radius = r;
    }

    public double area() {
        return Math.PI * radius * radius;
    }
}

// Usage:
Circle c = new Circle(5.0);
double a = c.area(); // 78.53981633974483

This models a circle as an object with a radius attribute and an area method. It’s a common exercise in introductory OOP.

Common Mistakes and How to Avoid Them

  • Using integer division: If you write int r = 5; and then r * r, it’s fine. But if you compute d/2 with integer diameter, you lose the decimal. Always use double for radius or diameter.
  • Forgetting to import Scanner: The Scanner class is in java.util. Without the import, the code won’t compile.
  • Not closing Scanner: It’s good practice to call input.close() to release resources, though for a small program it’s not critical.
  • Using 3.14 instead of Math.PI: This gives a less accurate result (e.g., 78.5 vs 78.54). Use Math.PI for full precision.
  • Mixing units: If the radius is in centimeters, the area is in square centimeters. Don’t mix meters and centimeters without converting.

Limits of the Formula

The formula A = πr² is exact for a perfect Euclidean circle. In real-world applications, it works for any circle as long as the radius is measured accurately. It does not account for:

  • Elliptical shapes (use A = πab where a and b are the semi-axes).
  • Surface area of a sphere (that’s 4πr²).
  • Measurement errors in the radius – a small error in r leads to a larger error in area because of the square.

For most educational and practical purposes, two decimal places are sufficient.

Frequently Asked Questions

How do I calculate the area if I only know the diameter?

Divide the diameter by 2 to get the radius, then use the same formula. Or use A = (π/4) × d² directly. For example, diameter 10 cm gives radius 5 cm, area ≈ 78.54 cm².

What is the difference between Math.PI and a literal 3.14159?

Math.PI is a double constant with about 15 decimal digits of precision. Using 3.14159 gives only 5 decimal places, which changes the result slightly (e.g., 78.53975 vs 78.53975 – actually the same to 5 decimals, but for large radii the difference grows). Always use Math.PI.

Can I use float instead of double?

Yes, but float has less precision (about 7 decimal digits) and you must append ‘f’ to literals (e.g., float r = 5.0f;). For most circle area calculations, double is preferred because it’s the default for floating-point in Java.

How do I round the area to a specific number of decimal places?

Use System.out.printf("%.2f", area) for two decimals, or Math.round(area * 100.0) / 100.0 for a rounded double value. The latter can be stored and used later.

Why does my program show 0.0 as the area?

This usually happens when you assign the result of an integer division to a double. For example, double r = 5/2; gives 2.0 because 5/2 is integer division. Use 5.0/2 or 5/2.0.

Is there a built-in method for circle area in Java?

No, Java’s standard library does not have a geometry area method. You must implement it yourself, but it’s trivial with Math.PI.

Related

Formula used
in r | Radius | cm | 5 | 0
out area | Area | cm² | pi * r^2 | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.