×

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

Insertion Sort in Java

Insertion Sort in Java Insertion sort in Java is a simple sorting algorithm that works in the same way as we hold cards in hand. Insertion sort does the sorting element-by-element,...

3 minutes read.

String to JSON in Java

Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the...

3 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Java String compareTo() Method

compareTo() method is used to compare the two specified Strings based on the alphabetical order(lexicographical order) of their characters.It returns positive number ,negative number or 0 Syntax: public int compareTo(String anotherString) Parameters: anotherString: the...

2 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

JDBC Architecture

JDBC: JDBC stands for Java Database Connectivity. Sun Microsystems has a specification called JDBC. JDBC is a Java API (Application Programming Interface) that enables users to interact or communicate with...

4 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

Java File Input Stream

Java makes use of stream ideas to speed up input and output processes. All packages of input and output streams are contained in java.io.package. Stream: A stream is nothing more...

5 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.