Javatpoint Logo
Javatpoint Logo

Java program to calculate area and circumference of circle

In this section, we will create a Java program to calculate the area and circumference of the circle.

Area of Circle Formulas

When the radius is known:

Java Program to Calculate Area and Circumference of Circle

When the diameter is known:

Java Program to Calculate Area and Circumference of Circle

When the circumference is known:

Java Program to Calculate Area and Circumference of Circle

Where,

A: is an area of a circle

π: is a constant, whose value is 3.1415 or 22/7.

r: is the radius of a circle

d: is the diameter of a circle

C: is the circumference of a circle

Let's implement the above formulas in a Java program and find the area of the circle.

In the following Java program, we have used a Java switch case because we have used three scenarios to find the area of the circle. The user provides an option and the switch statement executes the corresponding case. The three scenarios are: if the radius is known, if the diameter is known, if the circumference is known.

Note: Throughout this section, we have used a constant Pi (π). To access the value of the constant, we have used a static field PI of the Math class. It is defined in Math class as follows:

The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter. Its value is 3.141592653589793. Instead of using Math.PI we can also write the value of pi (3.14 or 22/7) directly.

AreaOfCircle.java

Output 1:

1. If the radius is known
2. If the diameter is known
3. If the circumference is known
Enter your choice: 1
Enter the radius of the circle: 6
The area of the circle is: 113.09733552923255

Output 2:

1. If the radius is known
2. If the diameter is known
3. If the circumference is known
Enter your choice: 2
Enter the diameter of the circle: 16
The area of the circle is: 201.06192982974676

Output 3:

1. If the radius is known
2. If the diameter is known
3. If the circumference is known
Enter your choice: 3
Enter the circumference of the circle: 30
The area of the circle is: 71.6197243913529

Output 4:

1. If the radius is known
2. If the diameter is known
3. If the circumference is known
Enter your choice: 4
invalid choice!

In the following Java program, we have calculated the circumference of the circle. The formula that we have used in the program is:

Circumference (C) =2πr

CircumferenceOfCircle.java

Output:

Enter the radius of the circle: 20
The circumference of the circle is: 125.66370614359172

In the following program, we have calculated the area and circumference of the circle in a single program.

AreaAndCircumferenceOfCircle.java

Output:

Enter the radius of the circle: 6
The area of the circle is: 113.09733552923255
The circumference of the circle is: 37.69911184307752

Next TopicJava Programs





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA