×

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

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Different Ways to Print Exception Message in Java

In this section, we will learn how to use various Java Throwable class methods to print exception messages. The Throwable class has the three methods listed below for printing the...

4 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Java Math nextAfter() Method

The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument. Syntax: public static double nextAfter (double start, double direction)public static float...

2 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Java Math atan2() Method

The atan2() method of Math class returns an angle theta from the conversion of rectangular coordinates to polar coordinates. Syntax: public static double atan2(double y, double x) Parameters: The parameter ‘y’ represents the ordinate...

3 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Compile time vs Runtime in java

Introduction: This article will discuss compile time vs. runtime in java. Compile time and runtime are two programming terms utilized in software improvement. Compile time is when the source code is...

3 minutes read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.