Once you click post assignment, learners in the selected class will immediately be able to see and complete this assignment
Back to Classroom
Square Root
Java Basics Course
Engineers From These Top Companies and Universities Trust EXLskills
1M+ Professionals | 100+ Institutions
This is the EXLskills free and open-source Java Basics Course. It guides learners via explanation, demonstration, and thorough practice, from no more than a basic understanding of Java, to a moderate level of essential coding proficiency.
-->
Is this course FREE?
Yes, this a 100% free course that you can contribute to on GitHub here!
One of the more popular Math class functions is the square root function. Like the floor() and ceil() methods, the square root method takes and returns the double datatype. It can be written as follows, Math.sqrt(25), and returns the rounded positive square root. Review the examples below.
SquareRootExample.java
package exlcode;
public class SquareRootExample {
public static double exampleVariableOne = Math.sqrt(100);
public static double exampleVariableTwo = Math.sqrt(6.25);
public static double exampleVariableThree = Math.sqrt(3);
public static void main(String[] args) {
System.out.println(exampleVariableOne);
System.out.println(exampleVariableTwo);
System.out.println(exampleVariableThree);
}
}
This method will return a double number even if the result is a perfect square. The error that will occur if we use a number less than zero is a result of "NaN", which stands for "Not a Number". Therefore, if we were to write Math.sqrt(-4), the return will be "NaN". Imaginary numbers cannot be represented or returned using this method. And lastly, keep in mind that this function will only return one positive square root value.