PI Constant in Java

In this tutorial, we will be going to discuss on the topic PI Constant in Java.

Also, we will be looking from where to use these functions and go into the deep study for the above topic, which is from where are these imported.

PI Constant in Java

As we already know how Java provides us with different functions and allow us to perform various operations.

PI is a built-in mathematical function that is part of Math class.

This math class is included in java.lang package.

Math.PI and others are modified with classifiers public.

As we know any field declared final that is its value cannot be changed after the initialization.

Now let us consider a program with usage of the PI constant.

import java.lang.Math.*;
public class Pi 
{
  public static void main(String[] args) 
  {
       		double r = 5;
       		double l = 15;
       		double area = r * r * Math.PI;
       		double volume = area * l;
       		System.out.println("Volume of cylinder is: " + volume);
      	}
}

Clarification in the above Program it can be seen that we imported the package required for including pi function.

On the other hand, inside the main block, we initialized the radius and the length as any value.

To sum up, now we calculate the area According to its formula and we used Math. The PI method here.

After this, we also calculated the volume and our objective is completed.

For example, below is sample output for the above program:

PI Constant in Java

Also, read:

Leave a Reply

Your email address will not be published. Required fields are marked *