What is Java Pi | Pi in Java | Math.PI Java Value and Example

Pi (π) is a well-known mathematical constant whose value is 22/7 or 3.14159265359. Programming languages also use this constant for mathematical calculation.

Here we discuss PI with respect to the Java programming language.

In Java PI is defined as a field in java.lang.Math class.

It is defined as

static double PI

and described as

The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter.

https://docs.oracle.com/

How to use PI in Java

PI is a static constant in Math class, so to access it we use the class name. PI. It can also be accessed using objects.

Math.PI

public class MyClass {
    public static void main(String args[]) {
     
      System.out.println("Value of PI = " + Math.PI);
    }
}
Value of PI = 3.141592653589793

Java Program to calculate Area of Circle using Math.PI

Area of Circle using Math.PI
Fig: Google Result for Area of Circle

Write a Java Program to calculate the Area of Circle

Below is the simple program to calculate the area of the Circle.

Program reads the value of radius using scanner method and stored in variable r.

Area is calculated using formula

area=Math.PI*r*r;

Area of Circle program in Java
Area of Circle program in Java
import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
     int r;
     Scanner sc=new Scanner(System.in);
     r=sc.nextInt();
     double area=Math.PI*r*r;
      System.out.println("Area of Circle is = " + area);
    }
}
Area of Circle is = 314.1592653589793

Write a Java program to calculate circumference of a Circle

circumference of a Circle
Fig: circumference of a Circle
import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
     
     int r;
     Scanner sc=new Scanner(System.in);
     r=sc.nextInt();
     double circumference=2*Math.PI*r;
      System.out.println("Circumference of Circle is = " + circumference);
    }
}
Circumference of Circle is = 62.83185307179586

The scanner class is used to read the values from user.

How to print PI (π) symbol in Java

import java.util.Scanner;

public class MyClass {
    public static void main(String args[]) {
     
    System.out.println("PI Symbol is \u03c0 ");
    System.out.println("PI Symbol is π  ");
    }
}
PI Symbol is π 
PI Symbol is π 

How to write pi in java

You can write Pi by using Unicode value ‘\u03c0’ or by using the string constant “π”.

     System.out.println("PI Symbol is \u03c0 ");
    System.out.println("PI Symbol is π  ");

How to use Math.PI in java

to use Math.pi Just use Math.PI as below

public class MyClass {
    public static void main(String args[]) {
          System.out.println("Value of PI  = " + Math.PI);
    }
}

Read More

  1. Duplicate Number between 1 to n numbers
  2. Print vowels in a String
  3. Program to reverse a string
  4. Prime number program in java using while loop
  5. Print vowels in a String
  6. Square Star Pattern
  7. Sum of two numbers using command line arguments in java