# Java sqrt Function

The Java sqrt Function is one of the Java Math Library functions used to find the square root of a specified expression or an individual double value.

## Java sqrt Function syntax

The basic syntax of the Math sqrt in Java Programming language to find the square root is as shown below. The following Java sqrt function will accept positive double value as an argument and returns the square root of the specified expression or Value.

```static double sqrt(double number); //Return Type is Double

// In order to use in program:
Math.sqrt(double number);```

Number: It can be a double value or a valid numerical expression for which you want to find the square root.

• If the number argument is positive double value, the Math.sqrt function will return the square root of a given value.
• If the number argument is negative double value, Math.sqrt function will return NaN.
• When it is not a number (NaN), java Math.sqrt function will return NaN.
• If it is positive infinity, Math.sqrt function will return the result as the Positive Infinity

## Java sqrt Function Example

The Java Math.sqrt Function allows finding the square root of a specified expression or an individual double value. In this math sqrt program, We are going to find the square root of different numbers, and display the output.

```// Java Math.sqrt Function
package MathFunctions;

public class SqrtMethod {
public static void main(String[] args) {
double a = Math.sqrt(10.90 + 15.10 - 22.50 + 1.50);
System.out.println("Square Root results = " + a);

System.out.println("\nSquare Root of Zero: " + Math.sqrt(0));

System.out.println("\nSquare Root of Positive Number: " + Math.sqrt(4));
System.out.println("Square Root of Positive Number: " + Math.sqrt(10.95));

System.out.println("\nSquare Root of Negative Number: " + Math.sqrt(-8.70));
System.out.println("Square Root of Negative Number: " + Math.sqrt(-5.00));
}
}```

First, We declared a variable of type Double and performed the Math.sqrt function directly on expression.

```double a = Math.sqrt(10.90 + 15.10 - 22.50 + 1.50);
System.out.println("Square Root results = " + a);```

Next, We used the sqrt Math function directly on the Positive double values.

```System.out.println("\nSquare Root of Positive Number: " + Math.sqrt(4));
System.out.println("Square Root of Positive Number: " + Math.sqrt(10.95));```

Next, We used the java Math.sqrt Function directly on Negative double values. As you can see from the Java screenshot, it is returning NaN for negative values.

```System.out.println("\nSquare Root of Negative Number: " + Math.sqrt(-8.70));
System.out.println("Square Root of Negative Number: " + Math.sqrt(-5.00));```

## Java sqrt on Array example

In this Java program, we show how to find the square roots of bulk data. To demonstrate the same, we declared an array of double type and found the square root of every element in an array.

```package MathFunctions;

public class SqrtMethodOnArrays {
public static void main(String[] args) {

double [] sqrtArray = {4.00, 9.00, 64.00, 100.00, -500.90, -124.8799, 1789.8597};

for (int i = 0; i < sqrtArray.length; i++) {
System.out.format("Square Root of Array Element = %.4f\n", Math.sqrt(sqrtArray[i]));
}
}
}```
``````Square Root of Array Element = 2.0000
Square Root of Array Element = 3.0000
Square Root of Array Element = 8.0000
Square Root of Array Element = 10.0000
Square Root of Array Element = NaN
Square Root of Array Element = NaN
Square Root of Array Element = 42.3067``````

Within this java sqrt example, first, We declared an Array of double type and assigned some random values.

`double [] sqrtArray = {4.00, 9.00, 64.00, 100.00, -500.90, -124.8799, 1789.8597};`

Next, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, compiler will check for the condition (i < sqrtArray.length).

TIP: sqrtArray.length finds the length of an array.

`for (int i = 0; i < sqrtArray.length; i++) {`

The following statements will print the output. If you observe the below code snippet, we used the Math.sqrt Function directly inside the System.out.format statement. Here, the Jcompiler calls the Java Math.sqrt method ( static double sqrt(double number) ) to find the square root of corresponding values.

`System.out.format("Square Root of Array Element = %.4f\n", Math.sqrt(sqrtArray[i]));`

If you want to find the square root of a single item, then use: Math.sqrt(myArray[index_position])

## Math sqrt function on Arraylist example

In this program, we are going to declare an ArrayList of double type. Next, we will find the square root of array list elements using Java sqrt function.

```package MathFunctions;

import java.util.ArrayList;

public class SqrtMethodOnArrayList {
public static void main(String[] args) {

ArrayList<Double> sqrtList = new ArrayList<Double>(5);

for (double x : sqrtList) {
System.out.println("Square Root of ArrayList =  " + Math.sqrt(x));
}
}
}```
``````Square Root of ArrayList =  111.11254654628343
Square Root of ArrayList =  NaN
Square Root of ArrayList =  12.287701168241355
Square Root of ArrayList =  44.3847946936786
Square Root of ArrayList =  44.910800482734665
Square Root of ArrayList =  NaN``````

In this Java sqrt example, we used the For Loop to iterate every double value in an ArrayList.

`for (double x : sqrtList) {`

The following statements will print the output. If you observe the code snippet, we used the Math.sqrt Function directly inside the System.out.format statement.

Here, the Jcompiler will call the Math.sqrt method ( static double sqrt(double x) ) to find the square root of corresponding values.

`System.out.println("Square Root of ArrayList =  " + Math.sqrt(x));`