math

# Calculate square root in Java with Math sqrt

This is an example of how to calculate the square root in Java, using the `sqrt(double a)` method of Math Class. The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Calculating the square root of a number implies that you should:

• Use the `sqrt(double a)` method of Math Class. The method gets a double parameter and returns the correctly rounded positive square root of the number.

## 1. What is Square root in Java

When a number multiplies itself, the product is the square number. The number is the square root. We get perfect square roots for a perfect square number. The √ sign represents a square root. If x^2 is a square number, then x is the square root of it.

For Example, 1^2 = 1, therefore square root of 1 is 1. 2^2 = 4, therefore square root of 4 is 2. Similarly, 9^2 = 81, therefore square root of 81 is 9 and so on. One interesting to know about square root is that when 9 square, it gives 81 and when −9 squares it also gives 81. So we can say 9 and -9 both are the square root of 81.

## 2. Properties of Square Root

• A perfect square root exists for a perfect square number only.
• The square root of an even perfect square is even.
• An odd perfect square will have an odd square root.
• A perfect square cannot be negative and hence the square root of a negative number is not defined.
• Numbers ending with (having unit’s digit) 1, 4, 5, 6, or 9 will have a square root.
• If the unit digit of a number is 2, 3, 7, or 8 then a perfect square root is not possible.
• If a number ends with an odd number of zeros, then it cannot have a square root. A square root is only possible for even number of zeros.
• Two square roots can be multiplied. √5, when multiplied by √2, gives √10 as a result.
• Two same square roots are multiplied to give a non-square root number. When √25 is multiplied by √25 we get 25 as a result.

## 3. Java Math.sqrt()

The class `java.lang.Math` contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Math.sqrt() is a static method and is part of `java.lang.Math` class. This method calculates and returns the square root of a given number. Below is the declaration of `Math.sqrt()` method-

```public static double sqrt(double a)
```

## 4. Example

Let’s take an example to understand better how the Math.sqrt() method works in java-

```package com.javacodegeeks.examples.math.sqrt;

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

//Square root of negative numbers
System.out.println("Result of Math.sqrt(-4): " + Math.sqrt(-4));
System.out.println("Result of Math.sqrt(-1): " + Math.sqrt(-1));

//Square root of positive | negative zero
System.out.println("Result of Math.sqrt(0): " + Math.sqrt(0));
System.out.println("Result of Math.sqrt(-0): " + Math.sqrt(-0));

//Square root of positive numbers
System.out.println("Result of Math.sqrt(1): " + Math.sqrt(1));
System.out.println("Result of Math.sqrt(4): " + Math.sqrt(4));

//Square root of non-perfect square  numbers
System.out.println("Result of Math.sqrt(14): " + Math.sqrt(14));
System.out.println("Result of Math.sqrt(-14): " + Math.sqrt(-14));

//Square root of positive | negative Infinity
System.out.println("Result Positive Infinity: " + Math.sqrt(1.0 / 0));
System.out.println("Result negative Infinity: " + Math.sqrt(-(1.0 / 0)));

}
}

```

In the above example, We have taken all kind of values negative, zero, positive, perfect square and non-perfect square. Let’s have a look over the output to and see how does Math.sqrt() behaves with these numbers-

```Result of Math.sqrt(-4): NaN
Result of Math.sqrt(-1): NaN
Result of Math.sqrt(0): 0.0
Result of Math.sqrt(-0): 0.0
Result of Math.sqrt(1): 1.0
Result of Math.sqrt(4): 2.0
Result of Math.sqrt(14): 3.7416573867739413
Result of Math.sqrt(-14): NaN
Result Positive Infinity: Infinity
Result negative Infinity: NaN
```

By seeing the output of the program, we can conclude below points about Math.sqrt() method-

• If the argument is NaN or less than zero, then the result is NaN.
• If the argument is positive, then the result is positive.
• If the argument is positive zero or negative zero, then the result is the same as the argument.
• Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

## 5. Conclusion

The square root is a common math operation which is required to implement other business logic. For Example, calculating diagonal of a right-angle triangle, implementing Pythagorean theorem, calculating trigonometric functions and etc.

This was an example of how to calculate the square root in Java, using the `sqrt(double a)` method of `Java.lang.Math` Class. Here is the source code of the examples used in this article.

You can download the full source code of this example here: Calculate square root in Java with Math sqrt

Last updated on Sept. 10, 2019

## Want to know how to develop your skillset to become a Java Rockstar? ### Vipul Kumar

Vipul is a Senior Software Engineer with experience in different software technologies including Java, Javascript, Angular, React, Material Design, databases (MySQL), HTML/CSS and even AWS, Big Data and Machine Learning. He likes learning new technologies and using the latest libraries in his work.
Subscribe
Notify of This site uses Akismet to reduce spam. Learn how your comment data is processed.