In this post, we will learn to code the Java Program to Calculate the Area of a Circle. Let’s understand about Circle and the formula to calculate the area of a given circle. Then we will learn How to calculate the area of a circle in Java Programming Language.
A Circle is defined as the set of points placed at an equal distance from a fixed point in a plane called a center.
Area of a Circle
The formula to calculate the area of a circle is as follows,
Area of a Circle, A = π r², where, π (pie) is equal to the fraction 22/7 and r stands for radius.
Now, let’s see the Java Program to Calculate the Area of a Circle.
Java Program to Calculate the Area of a Circle
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the radius of the circle : "); float radius = sc.nextFloat(); float area = (22.0f/7.0f) * radius * radius; System.out.println("Area of the Circle is "+ area +" sq. units"); } }
Output
Enter the radius of the circle : 7
Area of the Circle is 154.0 sq. units
Enter the radius of the circle : 10
Area of the Circle is 314.2857 sq. units
How Does This Program Work ?
Scanner sc = new Scanner(System.in); System.out.println("Enter the radius of the circle : "); float radius = sc.nextFloat();
In this program, we have taken the input of the radius from the user and stored it in the variable radius of datatype float using the Scanner Class in Java.
float area = (22.0f/7.0f) * radius * radius;
Then, we calculated the area of the circle using the formula, i.e. Area of a Circle, A = π r², where, π (pie) is equal to the fraction 22/7 and r stands for radius and we stored it in the variable named area of float datatype because pie is a decimal value.
System.out.println("Area of the Circle is "+ area +" sq. units");
Then, we printed the result using the System.out.println()
function to print in Java. This is the Java Program to Calculate the area of a circle.
Conclusion
I hope after going through this post, you understand how to code Java Program to Calculate the Area of a Circle.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Also Read: