Java sqrt() method with Examples
The java.lang.Math.sqrt() returns the square root of a value of type double passed to it as argument. If the argument is NaN or negative, then the result is NaN. If the argument is positive infinity, then the result is positive infinity. If the argument passed is positive zero or negative zero then the result will be same as that of the argument.
Syntax:
public static double sqrt(double a) Parameter : a : the value whose square root is to be returned. Return : This method returns the positive square root value of the argument passed to it.
Example 1: To show working of java.lang.Math.sqrt() method.
// Java program to demonstrate working // of java.lang.Math.sqrt() method import java.lang.Math; class Gfg { // driver code public static void main(String args[]) { double a = 30; System.out.println(Math.sqrt(a)); a = 45; System.out.println(Math.sqrt(a)); a = 60; System.out.println(Math.sqrt(a)); a = 90; System.out.println(Math.sqrt(a)); } } |
Output:
5.477225575051661 6.708203932499369 7.745966692414834 9.486832980505138
Example 2: To show working of java.lang.Math.sqrt() method when argument is NaN or +infinity.
// Java program to demonstrate working // of java.lang.Math.sqrt() method import java.lang.Math; // importing java.lang package public class GFG { public static void main(String[] args) { double positiveInfinity = Double.POSITIVE_INFINITY; double negativeVal = -5; double nan = Double.NaN; double result; // Here argument is negative, // output will be NaN result = Math.sqrt(negativeVal); System.out.println(result); // Here argument is positive infinity, // output will also positive infinity result = Math.sqrt(positiveInfinity); System.out.println(result); // Here argument is NaN, output will be NaN result = Math.sqrt(nan); System.out.println(result); } } |
Output:
NaN Infinity NaN
Recommended Posts:
- BigDecimal sqrt() Method in Java with Examples
- StrictMath sqrt() Method in Java
- BigInteger sqrt() Method in Java
- Java Guava | sqrt(long x, RoundingMode mode) of LongMath Class with Examples
- Java Guava | sqrt(int x, RoundingMode mode) method of IntMath Class
- BigIntegerMath sqrt() function | Guava | Java
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



