Hello guys, in today’s post we will learn to write Java program to calculate area and circumference of circle. Writing Java program for calculating area and circumference of circle is very easy. So let’s start and see how to do it in Java.
To find area and circumference of circle first we need to know the formula. Here is the formula –
Area of circle = πr2 square units
Circumference of circle = 2πr
Where,
- r is the radius of the circle.
- π is a constant whose value is 3.1415 or 22/7.
Now let’s learn how to calculate them in Java.
Java Program to Calculate Area and Circumference of Circle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package java_programs_example; import java.util.*; public class Circle { public static void main(String[] args) { double r, ar, circumference; Scanner in = new Scanner(System.in); System.out.println(" Enter the radius of circle :"); r = in.nextDouble(); ar = Math.PI * r * r; // calculating area of circle circumference = 2 * Math.PI * r; // calculating circumference of circle System.out.println(" Area of circle : " + ar +" square units"); System.out.println(" Circumference of circle : " + circumference); } } |
What we did ?
We have written Java program to calculate area and circumference of circle successfully. Now we will explain what we did to find area and circumference of a circle in Java.
- First of all we imported util package.
- Then we have created a public class named as Circle.
- Inside Circle class we created main() method.
- Then we have declared variables which are used in this program.
- Now we created object of the Scanner class.
- Then asked the user to enter the radius of circle.
- In next line read the value of radius entered by the user.
- Then calculated area of circle using formula Math.PI * r *r. Instead of using Math.PI we can also write the value of pi (3.14 or 22/7) directly.
- In next line calculated circumference of circle using formula 2 * Math.PI * r
- Then at last printed area and circumference of circle on screen.
Output:
1 2 3 4 |
Enter the radius of circle : 2 Area of circle : 12.566370614359172 square units Circumference of circle : 12.566370614359172 |
So guys, this was Java program to calculate area and circumference of circle. In the next tutorial, you will learn how to write Java Program to calculate Area of Triangle. THANKS.
Related Tutorials…
- Java Program to calculate Area of Triangle using heron’s formula
- Java Program to calculate Area and Perimeter of Right Triangle
- Java Program to calculate Area and Perimeter of Rectangle
- Java Program to calculate Area and Perimeter of Square
- Java Prorgram to calculate Area and Perimeter of Parallelogram
- Java Program to calculate Area of Trapezium
- Java Program to calculate Area and Volume of Cube
- Java Program to calculate Area and Volume of Cuboid
- Java Program to calculate Total Surface Area and Curved Surface Area of cylinder
- Java Program to calculate Volume of Cone
- Java Program to calculate Surface Area and Volume of Sphere