# Java sqrt Function

The Java sqrt Function is one of the 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 sqrt function will accept a 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 a positive double value, the Java math sqrt function will return the square root of a given value.
• If the number argument is a negative double value, it will return NaN.
• When it is not a number (NaN), it will return NaN.
• If it is positive infinity, it 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 program, we will find the square root of different numbers and display the output.

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

For the next two statements, We used the Math function directly on the Positive double values.

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

```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));
}
}```

## Java sqrt function to find square root of an Array items

In this program, we show how to find the square roots of bulk data. To demonstrate the same, we declared an array of double types 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 Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, the 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 Java Math.sqrt Function directly inside the System.out.format statement. Here, the Jcompiler calls the 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])

## Java sqrt function to find square root of ArrayList

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 the Java sqrt function.

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

The System.out.println 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 method ( static double sqrt(double x) ) to find the square root of corresponding values.

```package MathFunctions;

import java.util.ArrayList;

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

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