Table of contents
1.
Introduction
2.
Working
3.
Problem Solution
4.
Program/Source Code
5.
Steps in Java Code
6.
Example
7.
Time and Space Complexity
7.1.
Time Complexity
7.2.
Space Complexity
8.
Frequently Asked Questions
8.1.
Why do we use Math.PI instead of a fixed value like 3.14?
8.2.
Can we use float instead of double for radius?
8.3.
What happens if we enter a negative radius?
9.
Conclusion
Last Updated: Mar 4, 2025
Easy

Java Program to Find the Area of a Circle

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Finding the area of a circle is a fundamental mathematical operation that can be easily implemented in Java. The formula for calculating the area of a circle is A = Ï€ × r², where r is the radius. Java provides the Math.PI constant to use the value of Ï€ accurately. 

Java Program to Find the Area of a Circle

In this article, you will learn how to write a Java program to calculate the area of a circle using user input.

Working

To calculate the area of a circle in  Java, we follow these steps:

  1. Take the radius as input – The user provides the radius of the circle.
     
  2. Use the formula – Apply the formula Area = Math.PI * radius * radius.
     
  3. Display the result – Print the calculated area.

Problem Solution

To calculate the area of a circle, we need to use the formula:  

Area = Ï€ × r²  


Here, Ï€ (pi) is a constant (approximately 3.14159), & r is the radius of the circle. In  Java, we can use the `Math.PI` constant from the `Math` class to get the value of Ï€. The radius can be provided by the user or hardcoded in the program.  

Let’s understand this into steps:  

1. Define the radius: The radius is the distance from the center of the circle to its edge. It can be a fixed value or taken as input from the user.  
 

2. Use the formula: Multiply Ï€ by the square of the radius.  
 

3. Display the result: Print the calculated area to the console.  
 

Now, let’s write a complete Java program to solve this problem.  

Program/Source Code

import java.util.Scanner; // Import the Scanner class for user input
public class AreaOfCircle {
    public static void main(String[] args) {
        // Step 1: Create a Scanner object to read input
        Scanner scanner = new Scanner(System.in);

        // Step 2: Ask the user to enter the radius
        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();

        // Step 3: Calculate the area using the formula
        double area = Math.PI  Math.pow(radius, 2);

        // Step 4: Display the result
        System.out.println("The area of the circle is: " + area);

        // Step 5: Close the scanner to free resources
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code

 

In this Code:
 

1. Importing Scanner:  

We use the `Scanner` class to take input from the user. It’s part of the `java.util` package, so we import it at the beginning of the program.  

 

2. Creating a Scanner Object:  

The `Scanner` object (`scanner`) is created to read input from the keyboard.  

 

3. Taking User Input:  

The program prompts the user to enter the radius using `System.out.print()`. The input is stored in a variable called `radius` of type `double`.  

 

4. Calculating the Area:  

The formula `Math.PI  Math.pow(radius, 2)` is used to calculate the area.  

  • `Math.PI` gives the value of Ï€ (approximately 3.14159).  
     
  • `Math.pow(radius, 2)` calculates the square of the radius.  

 

5. Displaying the Result:  

The calculated area is printed to the console using `System.out.println()`.  

 

6. Closing the Scanner:  

It’s good practice to close the `Scanner` object after use to free up system resources.  

 

Example Output

If the user enters a radius of 5, the output will be:  

Enter the radius of the circle: 5  
The area of the circle is: 78.53981633974483  

Steps in Java Code

  1. Import the Scanner class to take input from the user.
     
  2. Read the radius value from the user.
     
  3. Use Math.PI (which holds the value of π) for accurate calculations.
     
  4. Calculate the area and display the result.

Example

Below is a simple Java program to calculate the area of a circle:

import java.util.Scanner;
public class CircleArea {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // Taking input from the user
        System.out.print("Enter the radius of the circle: ");
        double radius = scanner.nextDouble();
        // Calculating the area
        double area = Math.PI * radius * radius;
        // Displaying the result
        System.out.println("The area of the circle is: " + area);
        scanner.close();
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Explanation:

  • The program uses Scanner to take user input.
     
  • It reads the radius as a double to support decimal values.
     
  • The formula Math.PI * radius * radius computes the area.
     
  • The result is printed using System.out.println().
     

Example Output:

Enter the radius of the circle: 5
The area of the circle is: 78.53981633974483

Time and Space Complexity

Time Complexity

  • The program executes a few simple operations:
    • Taking input (O(1))
       
    • Performing the mathematical calculation (O(1))
       
    • Printing the result (O(1))
       
  • Since all these operations run in constant time, the overall time complexity is O(1).

Space Complexity

  • The program uses three variables: radius, area, and a Scanner object.
     
  • As these require only a fixed amount of memory, the space complexity is O(1).

Frequently Asked Questions

Why do we use Math.PI instead of a fixed value like 3.14?

Using Math.PI provides higher accuracy compared to using a rounded value like 3.14.

Can we use float instead of double for radius?

Yes, but double is preferred as it provides better precision.

What happens if we enter a negative radius?

The program will still compute an area, but in practical cases, a negative radius is invalid. You can add validation to handle such cases.

Conclusion

In this article, we discussed how to find the area of a circle in Java using the formula Area = Ï€ × r², where r is the radius. We implemented a simple Java program using Math.PI to get an accurate value of Ï€. Understanding this concept helps in handling mathematical computations efficiently in Java applications.

Live masterclass