PI in Java

The Pi is a constant value in Mathematics that represents a floating-point value 3.1415. It is mostly used in geometry to calculate area, circumference, volume, etc. If you have studied geometry in your academic, then you may be aware of use of Pi such as calculating area require this constant value.

In Java, when we do geometric calculations, then we need this constant and for that, we have mainly two options either we create a constant and assigned 3.1415 value to it or use PI constant of Math class. Yes, Java provides a Math class that contains PI constant. So, Let’s see some examples to understand the use of PI in Java.

PI Constant in Java

Here, we created a constant our own and assigned a value to it. We declared it as final so, cannot be changed. Here, we are calculating the area and circumference of circle by using the created PI constant.

Output

Enter radius :
5
Area is : 78.53
Circumference is : 31.41

PI Constant in Java Math Class

Since Math class provides PI constant, we can use it to perform geometric calculations. Here, we are getting area and circumference of area using Math class. See the example and output below.

Output:

Enter radius :
5
Area is : 78.53
Circumference is : 31.41

That’s all about PI in java.

Related Posts

  • 05 October

    Calculate total surface area of Cylinder in java

    In this post, we will see how to calculate total surface area of Cylinder in java. Formula of calculating total surface area of Cylinder is: Surface area of Cylinder = 2 *Π * r * r + 2 *Π * r * h Where r is radius of cylinder and h is height of cylinder […]

  • 05 October

    Calculate total surface area of Hemisphere in java

    In this post, we will see how to calculate total surface area of Hemisphere in java. Hemisphere is exactly half of sphere. There can be many practical examples of Hemisphere. You can divide earth into two hemisphere i.e. Northen Hemisphere and Southern Hemisphere. Formula of calculating total surface area of Hemisphere is: surface area of […]

  • 05 October

    Get random number between 0 and 1 in java

    In this post, we will see how to get random number between 0 to 1 in java. We have already seen random number generator in java. We can simply use Math.random() method to get random number between 0 to 1. Math.random method returns double value between o(inclusive) to 1(exclusive). [crayon-6001ccbd9e590972773583/] When you run above program, […]

  • 03 January

    java round double/float to 2 decimal places

    In this post, we will see how to round double to 2 decimal places in java. There are many ways to do it.Let’s go through few ways. Let’s understand each with the help of simple example. Using Math.round(double*100.0)/100.0 [crayon-6001ccbd9e757488213841/] Output: Rounded double: 2343.55 Rounded float: 2343.55 You must be wondering how this works. double*100.0 – […]

  • 03 January

    How to get square root of number in java

    In this tutorial. we will see if how to get square root of number in java. It is very simple to get square root of number in java. You can simply use Math’s sqrt() method to calculate square root of number. Syntax [crayon-6001ccbd9ea6c583292679/] Return type It returns square root of the number. Square root of […]

  • 07 September

    Power function in java

    Power function in java is used to get first argument’s power to 2nd argument. For example: Let’s say you want to calculate 2 to the power 4. That will be 2*2*2*2=16 In java, you can do it by : Math.pow(2,4) =16 Syntax [crayon-6001ccbd9ed27177300755/] Example : Let’s use power function in java. [crayon-6001ccbd9ed2c185857930/] When you run […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.