
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Perimeter of a Circle in Java
For a given circle with radius "r", write a Java program to find the perimeter of that circle. The circumference is also known as the perimeter. It's the distance around a circle.
Circumference is given by the formula C = 2?r where, pi/? = 3.14 and r is the radius of the circle ?
Example Scenario
Input: radius = 5 Output: perimeter = 31.400000000000002
2 * 3.14 * 5 = 31.428571428571427
By Defining Constant Value for PI
In this Java program, we define the value of PI as a constant and find the perimeter of circle using it.
public class PerimeterOfCircle { // defining constant static final double PICONST = 3.14; public static void main(String args[]) { double my_radius, my_perimeter; my_radius = 5; System.out.println("The radius of the circle is defined as " +my_radius); my_perimeter = PICONST * 2 * my_radius; System.out.println("The perimeter of Circle is: " + my_perimeter); } }
Output
The radius of the circle is defined as 5.0 The perimeter of Circle is: 31.400000000000002
Using Math.PI
In the following example, we use the Math.PI to calculate the perimeter of the circle.
public class PerimeterOfCircle { public static void main(String args[]) { double my_radius, my_perimeter; my_radius = 7; System.out.println("The radius of the circle is defined as " + my_radius); my_perimeter = Math.PI * 2 * my_radius; System.out.println("The perimeter of Circle is: " + my_perimeter); } }
Output
The radius of the circle is defined as 7.0 The perimeter of Circle is: 43.982297150257104
Advertisements