×

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;  
        int radius = 10; 
        double area = pi*(radius*radius);   
        System.out.println("Area of the circle for given radius" + radius + " = " + area);  
    }  
}  

Output:

Java Pi

Circumference.java

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

Output:

Java Pi

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) {
              int radius = 10;
   System.out.println(" The circumference of a circle for given radius " + radius + "       = " + circleCircumference(radius));
}
}

Output:

Java Pi

PiExp2.java

public class PiExp2 {


public static double circleArea(int radius) {


return Math.PI * Math.pow(radius, 2);
}
 public static void main(String[] args) {
       int radius = 10;
System.out.println(" The area of the circle for given radius " + radius + " = " + circleArea(radius));
}
}

Output:        

Java Pi

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) {


int radius = 10;


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

Output:

Java Pi

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) {
int radius = 10;
System.out.println(" The surface area of the sphere for given radius " + radius + " = " + sphereSurfaceArea(radius));
}
}

Output:

Java Pi

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:

Java Pi

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.


Related Topics

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

Java Substring

What is a substring in Java? Here by the name  " substring " itself, we can easily come to know it is a part of a string or a subset of...

4 minutes read.

Short Circuit Logical Operators in Java

When there are two or more relational expressions in a decision-making statement, logical operators are utilized to combine them. The logical operators short circuit and not-short circuit fall into two...

5 minutes read.

Heap Sort in Java

Heap Sort in JavaHeap sort in Java uses the data structure binary heap, min-heap, or max heap to do the sorting of elements. Since min-heap always gives the minimum element first,...

8 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Java Integer decode() method

The decode() method of Integer class decodes a String into an Integer. It can accept decimal, hexadecimal and octal numbers. Syntax public static Integer decode(String nm) throws NumberFormatException Parameters The parameter ‘nm’ represents the...

2 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value. Syntax public int hashCode()public int hashCode(boolean value) Parameters The parameter ‘value’ represents the value...

2 minutes read.

Java Arrays

An array is an object that stores a collection of values. An array can store two types of collection data: objects and primitives. An array is a collection of values which...

2 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.