Java program to calculate area and circumference of circleIn this section, we will create a Java program to calculate the area and circumference of the circle. Area of Circle FormulasWhen the radius is known: When the diameter is known: When the circumference is known: 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 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India