What is Circle?
A circle is a round shaped figure that has no corners or edges. If you have a radius for a circle, you could easily find out Area, Diameter and Circumference for a Circle.
Here are the formulas for the Circle.
- Area: A=πr2 (π = 3.14)
- Perimeter (Circumference): C=2πr (π = 3.14)
- Diameter: d=2r
Here is a simple Java program to find out Diameter, Area and Circumference of any given circle provided you have radius.
Create class CrunchifyCircleDiameterAreaCircumference.java
package crunchify.com.java.tutorials; import java.text.DecimalFormat; import java.util.Scanner; /** * @author Crunchify.com * In Java How to find Diameter, Area and Circumference for a Circle? */ public class CrunchifyCircleDiameterAreaCircumference { public static void main(String[] args) { Scanner sc = new Scanner(System.in); crunchifyPrint("Enter a Circle Radius: "); double crunchifyRadius = sc.nextDouble(); // Diameter = d=2r // Area = 2 x crunchifyRadius double crunchifyDiameter = 2 * crunchifyRadius; crunchifyPrint("===> Diameter: " + crunchifyDiameter); DecimalFormat df = new DecimalFormat("0.000000"); // Area = A=πr2 (π = 3.14) // Area = PI * crunchifyRadius * crunchifyRadius double crunchifyArea = Math.PI * (crunchifyRadius * crunchifyRadius); crunchifyPrint("===> Area: " + df.format(crunchifyArea)); // Circumference = C=2πr (π = 3.14) // Circumference = 2 * PI * crunchifyRadius double crunchifyCircumference = Math.PI * 2 * crunchifyRadius; crunchifyPrint("===> Circumference: " + df.format(crunchifyCircumference)); } private static void crunchifyPrint(String s) { System.out.println(s); } }
Result:
Just run above program as a Java Program in Eclipse or IntelliJ IDEA.
Enter a Circle Radius: 5 ===> Diameter: 10.0 ===> Area: 78.539816 ===> Circumference: 31.415927 Process finished with exit code 0
Enter a Circle Radius: 10 ===> Diameter: 20.0 ===> Area: 314.159265 ===> Circumference: 62.831853 Process finished with exit code 0
Let us know if you face any issue running above program.
The post How to find Diameter, Area and Perimeter (Circumference) for a Circle in Java? appeared first on Crunchify.
0 Commentaires