- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java sqrt() method with Examples
The java.lang.Math.sqrt(double a) returns the correctly rounded positive square root of a double value. Special cases −
If the argument is NaN or less than zero, then the result is NaN.
If the argument is positive infinity, then the result is positive infinity.
If the argument is positive zero or negative zero, then the result is the same as the argument.
Following is an example to implement the sqrt() method of the Math class in Java −
Example
import java.lang.*; public class Demo { public static void main(String[] args) { // get two double numbers numbers double x = 9; double y = 25; // print the square root of these doubles System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x)); System.out.println("Math.sqrt(" + y + ")=" + Math.sqrt(y)); } }
Output
Math.sqrt(9.0)=3.0 Math.sqrt(25.0)=5.0
Example
Let us now see another example to implement the sqrt() method with negative and other value −
import java.lang.*; public class Demo { public static void main(String[] args) { // get two double numbers numbers double x = -20.0; double y = 0.0; // print the square root of these doubles System.out.println("Math.sqrt(" + x + ")=" + Math.sqrt(x)); System.out.println("Math.sqrt(" + y + ")=" + Math.sqrt(y)); } }
Output
Math.sqrt(-20.0)=NaN Math.sqrt(0.0)=0.0
- Related Articles
- Java toDegrees() method with Examples
- Java signum() method with Examples
- Java lang.Long.toBinaryString() method with Examples
- Java cbrt() method with Examples
- Java ceil() method with Examples
- Java floor() method with Examples
- Java streams counting() method with examples
- Java Signature getAlgorithm() method with Examples
- Java Signature getInstance() method with Examples
- Java Signature getProvider() method with Examples
- Java Signature initSign() method with Examples
- Java Signature toString() method with Examples
- Java lang Integer.toHexString() Method with Examples
- Java lang Long.toOctalString() Method with Examples
- Matcher start() method in Java with Examples

Advertisements