Calculating the area of a circle is a basic problem in Java that uses simple mathematical operations. To calculate the area, we need the radius of the circle. The area is calculated using the formula:
Area=π×radius2
Here, we take 𝜋=3.142
Example:
Input: radius = 5
Output: Area of circle is: 78.55
Input: radius = 8
Output: Area of circle is: 201.08
This Java program calculates and displays the area of a circle using a given radius.
public class GFG {
public static void main(String[] args)
{
int radius;
double pi = 3.142, area;
radius = 5;
area = pi * radius * radius;
System.out.println("Area of circle is: " + area);
}
}
Output
Area of circle is: 78.55
Explanation:
- The radius is stored in the variable radius.
- The value of π is assigned to pi as 3.142.
- The area is calculated using area = pi * radius * radius.