# Java Pi

## What is Pi?

There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a mathematical constant that can be determined.

A constant pi has a value of about 3.14. Pi has a built-in constant field in Java that is part of the Math class (java.lang).

## What is Math.PI in Java?

In Java, the static final double constant Math.PI is the same as in mathematics. In Math class, the Math.PI constant is used to perform a variety of mathematical and scientific computations, such as determining the surface area and volume of a sphere or the area and circumference of a circle. The "pi" quantity has a crucial role and countless applications in real life. A list of some of them is provided below.

Pi is used by aerospace engineers to determine the size of an aircraft's body.

Pi helps medical science by enabling analysis of the anatomy of the eye.

Pi is used by biochemists to analyze the structure of DNA.

To predict the state's population dynamics, statisticians use the pi.

The following programmes, which do not make use of the built-in constant field, show how to use the constant value pi.

Area.java

``````import java.util.Scanner;
public class Area
{
public static void main(String ar[])
{

final double pi = 3.14;
System.out.println("Area of the circle for given radius" + radius + " = " + area);
}
}  ``````

Output:

Circumference.java

``````import java.util.Scanner;
public class Circumference
{
public static void main(String ar[])
{  final double pi = 3.14;
System.out.println("Circumference of the circle for given radius" + radius + " = " + circumference);
}
}
``````

Output:

The final double variable pi in the aforementioned codes is used to set the pi value to 3.14. Additionally, the area and circumference are computed and shown in the above outputs.

Now let's look at the following executable examples to see how to obtain and use the value of Math.PI in Java.

PiExp1.java

``````public class PiExp1 {
public static double circleCircumference(int radius) {
return Math.PI * (2 * radius);
}
public static void main(String[] args) {
System.out.println(" The circumference of a circle for given radius " + radius + "       = " + circleCircumference(radius));
}
}``````

Output:

PiExp2.java

``````public class PiExp2 {

public static double circleArea(int radius) {

}
public static void main(String[] args) {
System.out.println(" The area of the circle for given radius " + radius + " = " + circleArea(radius));
}
}``````

Output:

PiExp3.java

``````public class PiExp3 {

public static double sphereVolume(int radius) {

return (4 / 3) * Math.PI * Math.pow(radius, 3);
}
public static void main(String[] args) {

System.out.println(" The volume of the sphere for given radius " + radius + " = " + sphereVolume(radius));
}
}
``````

Output:

PiExp4.java

``````public class PiExp4 {
public static double sphereSurfaceArea(int radius) {

return 4 * Math.PI * Math.pow(radius, 2);
}

public static void main(String[] args) {
System.out.println(" The surface area of the sphere for given radius " + radius + " = " + sphereSurfaceArea(radius));
}
}``````

Output:

A program that uses both user-defined variables and built-in variables to calculate the volume of a cylinder.

BvsU.java

``````import java.lang.Math.*;
public class BvsU
{
public static void main(String[] args)
{
final double pi=3.14;
double r = 5;
double l = 15;
double a = r * r * Math.PI;
double v = a * l;
System.out.println("Volume of cylinder by using the built-in variable PI is: " + v);
double a1 =r * r * pi;
double v1 = a1 * l;
System.out.println("Volume of cylinder by using the user-defined Pi value is: " + v1);
}
}  ``````

Output:

Both approaches to utilizing the Pi constant in a program are shown in the aforementioned Java code. Both methods are used to calculate and display the area of a cylinder using a multiplication operation.

This article has discussed the Pi mathematical constant, the Java programme that implements it, and a programme that uses it as an example.