Java Math.PI

Java Math.PI is a public static constant in Math class. PI constant is high-precision approximation (3.141592653589793) and usable in any Java code.

Java Math.PI

Example 1: Print PI number using Math.PI constant.

System.out.println(Math.PI);
//3.141592653589793

Example 2: Find the area and perimeter of a circle for a given radius (as user input)

Scanner input = new Scanner(System.in);
System.out.print("─▒nput a value for radius > ");
double radius = input.nextDouble();

double perimeter = 2 * Math.PI * radius;
double area = Math.PI * radius * radius;

System.out.println("Perimeter of the cicle is: " + perimeter);
System.out.println("Area of the cicle is: " + area);

Console Output:

input a value for radius > 15
Perimeter of the cicle is: 94.24777960769379
Area of the cicle is: 706.8583470577034