Java Program to Calculate Area of Circle

In this tutorial, you will learn how to calculate the area of a circle in Java. We will cover three different approaches:

  • Basic approach (hardcoded values)
  • User input (using the Scanner class)
  • Modular approach (using user-defined methods)

Let’s explore each method step by step with code examples. Before starting to write a Java program to find the area of a circle, we will first understand the formulas of them.

Formula for Area of a Circle

The formula for calculating the area of a circle is:

Area = π × radius × radius
or
Area = πr²

In Java, we can use Math.PI to obtain the value of π. For example, if the radius of a circle is 5:

Area of circle = π × 5 × 5 = 78.54 square units

Basic Java Program to Calculate Area of a Circle

Problem Statement: Write a Java program to calculate the area of a circle without taking input from the user and display the result on the console.

The program takes the radius of a circle as input and calculates its area using the standard mathematical formula.

This is the basic approach, which requires only basic input, variables, multiplication, and output, making it suitable for beginners learning Java. It calculates the area directly inside the main() method using a hardcoded value for the radius.

Complete Java Program to Calculate Area of a Circle

The following Java program uses a hardcoded value for the radius of the circle. It calculates and displays the area of the circle.

// Program to calculate the Area of a Circle.
public class CircleBasic {
    public static void main(String[] args) {

        // Declare and initialize the radius of the circle
        double radius = 5.0;

        // Calculate the area using the formula: Area = PI * radius * radius
        double area = Math.PI * radius * radius;

        // Display the radius and calculated area
        System.out.println("--- Circle Details ---");
        System.out.println("Radius: " + radius);
        System.out.println("Area of Circle: " + area);
    }
}

Output:

--- Circle Details ---
Radius: 5.0
Area of Circle: 78.53981633974483

Explanation of the Program

Step 1: Declare and initialize the radius

double radius = 5.0;

Here, we declare a variable named radius using the double data type and initialize it with 5.0. Using double allows the program to work with both whole numbers and decimal values.

Step 2: Calculate the area of Circle

double area = Math.PI * radius * radius;

Here:

  • Math.PI represents the mathematical constant π.
  • radius * radius calculates the square of the radius.
  • After multiplying, the result is stored in the area variable.

Step 3: Display the result

System.out.println("Area of Circle: " + area);

The println() method displays the calculated area on the console.

Java Program to Calculate Area of Circle Using Scanner

In the previous program, we have directly assigned value to the variable radius. Now, we will accept the radius of a circle from the user at runtime. For this purpose, we use Java’s Scanner class.

import java.util.Scanner;
/**
 * Program to calculate the Area of a Circle
 * using user input.
 */
public class CircleScanner {
    public static void main(String[] args) {

        // Create Scanner object to read input from the keyboard
        Scanner scanner = new Scanner(System.in);
        // Read the radius from the user
        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();

        // Calculate the area using the formula: Area = PI * radius * radius
        double area = Math.PI * radius * radius;
        // Display the calculated area
        System.out.println("Area of Circle: " + area);

        // Close the Scanner
        scanner.close();
    }
}

Output:

Enter the radius of the circle: 7
Area of Circle: 153.93804002589985

In this program:

  • The statement import java.util.Scanner; imports the Scanner class, which is used to read input from the keyboard.
  • new Scanner(System.in) creates a Scanner object for reading input from the standard input stream (keyboard).
  • nextDouble() reads the radius entered by the user as a double value.
  • Math.PI provides the mathematical constant π (approximately 3.14159).
  • Math.PI * radius * radius calculates the area of the circle using the formula πr².
  • scanner.close() closes the Scanner object and releases the resources associated with it.

Calculate the Area of a Circle in Java Using User-Defined Method

Instead of placing the calculation logic for the area inside the main() method, we can create a separate method to calculate the area of a circle. This approach makes the calculation logic reusable and keeps the main() method cleaner.

/**
 * Program to calculate the Area of a Circle
 * using a user-defined static method.
 */
public class CircleMethods {
    // User-defined static method to write the calculation logic
    public static double calculateArea(double radius) {
        return Math.PI * radius * radius;
    }

    public static void main(String[] args) {

        // Declare and initialize the radius
        double radius = 7.0;

        // Call the user-defined method
        double area = calculateArea(radius);

        // Display the radius and calculated area
        System.out.println("--- Circle Details ---");
        System.out.println("Radius: " + radius);
        System.out.println("Area: " + area);
    }
}

Output:

--- Circle Details ---
Radius: 7.0
Area: 153.93804002589985

The advantage of this approach is that the calculation logic is separated into the reusable calculateArea() method.

Important Points to Remember

  • A circle requires only one dimension: the radius.
  • The formula for calculating the area of a circle is π × radius × radius.
  • In Java, Math.PI can be used to represent the mathematical constant π.
  • Use the double data type when the radius may contain decimal values.
  • Scanner.nextDouble() can be used to read the radius entered by the user from the keyboard.
  • The calculateArea() method can be used to make the area calculation reusable.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.