# Calculate Area of a Circle in Java

In this article, we will see how to calculate area of a circle in Java. Calculating the area of a circle is a common mathematical task, and it can be particularly useful in various applications such as geometry, physics, and engineering. Let’s dive in.

## Problem Statement

Given the radius of a circle. Write a program to calculate area of circle in java. The input will include the radius, and the output should be the area of the circle. For Example:

Input:

Radius of the circle: 5.0 units

Output:

Area of the circle: 78.54 square units (rounded to two decimal places)

## Solution

Area of a Circle Formula: The area (A) of a circle can be calculated using the formula: A = π * r^2, where π (pi) is a mathematical constant approximately equal to 3.14159, and r is the radius of the circle.

### Algorithm

• Accept the radius of the circle as input.
• Calculate the area using the formula: A = π * r^2.
• Java provides the `Math.PI` constant to represent the value of π.
• Use Math.pow() method to calculate square of radius
• Display the calculated area.

### Java Program To Calculate Area Of Circle

``````// Java Program To Calculate Area Of Circle

import java.util.Scanner;

public class CalculateCircleArea {
public static void main(String[] args) {
// Create a Scanner for user input
Scanner scanner = new Scanner(System.in);

// Prompt the user to enter the radius
System.out.print("Enter the radius of the circle: ");

// Calculate the area of the circle
double area = Math.PI * Math.pow(radius, 2);

// Display the calculated area
System.out.printf("The area of the circle is: %.2f square units%n", area);

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

Output

``````Enter the radius of the circle: 5.0
The area of the circle is: 78.54 square units``````

## Key Takeaways

• The area of a circle can be calculated using the formula A = π * r^2, where π is a mathematical constant (approximately 3.14159) and r is the radius of the circle.
• Java provides the `Math.PI` constant to represent the value of π.

Similar Posts

Add Days To The Current Date in Java

Calculate Area of Hexagon in Java

Program to Calculate EMI in Java

Reverse A Number In Java

Leap Year Program In Java

Checkout more Java Tutorials here.

I hope You liked the post ?. For more such posts, ? subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

## FAQ

How can I handle invalid input, such as a negative radius or non-numeric input?

What is the difference between `Math.PI` and a predefined π value?

Are there any Java libraries or frameworks for advanced geometric calculations, including circles?

## 90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

## Related Articles

### Different Ways To Add Two Numbers In Java

Addition two numbers in java is a fundamental concept in programming. This article explains different techniques to add two numbers in Java. We will discuss: So, let’s get started. Addition

### Access Specifiers In Java With Examples

Access modifiers or access specifiers in Java control access to classes, methods, and member variables. They determine the visibility and accessibility of class members. Choosing the right access level is