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
- Import the Scanner class to take input from the user.
- Read the radius value from the user.
- Use Math.PI (which holds the value of π) for accurate calculations.
- 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.