Table of contents
1.
Introduction
2.
Syntax
3.
Examples
3.1.
Example 1: Finding the square root of a positive number
3.2.
Java
3.3.
Example 2: Finding the square root of a variable
3.4.
Java
3.5.
Example 3: Calculating the hypotenuse of a right triangle
3.6.
Java
4.
Frequently Asked Questions
4.1.
What happens if I try to find the square root of a negative number using Math.sqrt()?
4.2.
Can I use Math.sqrt() with other numeric data types besides double?
4.3.
Is there any performance difference between using Math.sqrt() and calculating the square root manually?
5.
Conclusion
Last Updated: Jul 29, 2024
Easy

Math.sqrt() Method in Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In programming, working with numbers is a fundamental task. One common operation is finding the square root of a number. In Java, there's a built-in method called Math.sqrt() that makes it easy to calculate square roots. 

Math.sqrt() Method in Java

In this article, we'll learn how to use Math.sqrt() in Java, along with examples to help you understand its use.

Syntax

The syntax for using the `Math.sqrt()` method in Java is very simple and easy. Let’s see how you can use it:

double result = Math.sqrt(number);


In this syntax, `number` is the value for which you want to find the square root. The `Math.sqrt()` method returns the square root of `number` as a `double` value, which is stored in the `result` variable.

It's important to note that the `Math.sqrt()` method always returns a `double` value, even if the input is an integer. If you need to work with integers specifically, you'll need to perform additional type casting.

Examples

Let's look at a few examples to see how `Math.sqrt()` can be used in practice.

Example 1: Finding the square root of a positive number

  • Java

Java

double number = 25;

double squareRoot = Math.sqrt(number);

System.out.println("The square root of " + number + " is " + squareRoot);
You can also try this code with Online Java Compiler
Run Code


Output

The square root of 25.0 is 5.0


In this example, we find the square root of the number 25 using `Math.sqrt()`. The result, which is 5, is stored in the `squareRoot` variable and then printed to the console.

Example 2: Finding the square root of a variable

  • Java

Java

int x = 16;

double squareRoot = Math.sqrt(x);

System.out.println("The square root of " + x + " is " + squareRoot);
You can also try this code with Online Java Compiler
Run Code


Output

The square root of 16 is 4.0


Here, we demonstrate that `Math.sqrt()` can also work with variables. We store the value 16 in the `x` variable and then pass `x` to `Math.sqrt()`. The result is stored in the `squareRoot` variable and printed to the console.

Example 3: Calculating the hypotenuse of a right triangle

  • Java

Java

double base = 3;

double height = 4;

double hypotenuse = Math.sqrt(base * base + height * height);

System.out.println("The hypotenuse is " + hypotenuse);
You can also try this code with Online Java Compiler
Run Code


Output

The hypotenuse is 5.0


In this example, we use `Math.sqrt()` to calculate the hypotenuse of a right triangle using the Pythagorean theorem. We have the base and height of the triangle stored in variables, and we compute the square of each using multiplication. Then, we add the squares together and pass the result to `Math.sqrt()` to find the hypotenuse.

Frequently Asked Questions

What happens if I try to find the square root of a negative number using Math.sqrt()?

If you pass a negative number to Math.sqrt(), it will return NaN (Not-a-Number), indicating that the result is undefined in the real number system.

Can I use Math.sqrt() with other numeric data types besides double?

Yes, you can use Math.sqrt() with float, int, and long data types. However, the method always returns a double value, so you may need to cast the result back to the desired data type.

Is there any performance difference between using Math.sqrt() and calculating the square root manually?

Math.sqrt() is a highly optimized method implemented in the Java standard library. It is generally faster and more efficient than calculating the square root manually using techniques like the Babylonian method or Newton's method.

Conclusion

In this article, we learned about the Math.sqrt() method in Java, which provides an easy way to find the square root of a number. We discussed the syntax of Math.sqrt() and looked at several examples showing its usage in different scenarios. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass