BigInteger sqrt() Method in Java - GeeksforGeeks

03 Mar, 2022
java.math.BigInteger.sqrt() is an inbuilt function added in Java SE 9 & JDK 9 which returns BigInteger value of square root of a BigInteger on which sqrt() method is applied. It is the same as the floor(sqrt(n)) where n is a number. This Square root is less than the real square root if the real square root can not representable as an integral value.
Syntax: 
public BigInteger sqrt()
Parameters: The method does not take any parameters. Return Value: Method returns the integer square root of this BigInteger. Exception: The method will throw ArithmeticException if BigInteger is negative.
Example:  
Input: 234876543456
Output: 484640
Explanation: 122 is given as input which is the bigInteger.
The square root of 122 is 11.04536
whose BigInteger equivalent is 11 and using sqrt()
method of BigInteger class we can get
Square root of any BigInteger.

Input: 122
Output: 11
Below programs illustrates sqrt() method of BigInteger class:
Program 1: Showing application of sqrt() method to get square root of 31739.  
Java
// Please run this program in JDK 9 or JDK 10// Java program to demonstrate sqrt() methodimport java.math.*; public class GFG {     public static void main(String[] args)    {         // Creating a BigInteger object        BigInteger big, squareRoot;         big = new BigInteger("31739");         // calculate square root o bigInteger        // using sqrt() method        squareRoot = big.sqrt();         // print result        System.out.println("Square root value of BigInteger " + big                           + " is " + squareRoot);    }}
Output: 
Square root value of BigInteger 31739 is 178
Program 2: Showing Exception is thrown by sqrt() method.  
Java
//Please run this program in JDK 9 or JDK 10// Java program to demonstrate sqrt() method//and exception thrown by itimport java.math.*; public class GFG {     public static void main(String[] args)    {         // Creating a BigInteger object        BigInteger big,squareRoot=null;         big = new BigInteger("-2345");         //calculate square root o bigInteger        //using sqrt() method        try {            squareRoot = big.sqrt();        } catch (Exception e) {            e.printStackTrace();        }         // print result        System.out.println("Square root value of BigInteger " + big                +" is "+squareRoot);    }}
Output: 
java.lang.ArithmeticException: Negative BigInteger
    at java.base/java.math.BigInteger.sqrt(Unknown Source)
    at GFG.main(GFG.java:19)
Square root value of BigInteger -2345 is null
Reference: https://docs.oracle.com/javase/9/docs/api/java/math/BigInteger.html#sqrt– 
shubham_singh
surinderdawra388
sultanmuradsafarov
Java 8
Java-BigInteger
Java-Functions
java-math
Java-math-package
Java
Java-BigInteger
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
Difference between Abstract Class and Interface in Java
How to remove an element from ArrayList in Java?