Java Math class provides several methods to perform several operations on math calculations like max(), min(), sin(), cos(), round(), ceil(), floor(), sqrt function etc. The java.lang.Math class contains various methods for performing basic numeric operations Here, in the page we will discuss about the sqrt() Function in java.
Java sqrt Function :
Java Collection contains Math class and The class Math contains methods for performing several basic and advance numeric operations such as the exponential, logarithm, square root, and trigonometric functions. The sqrt() method takes an argument, say num1, and returns the square root of the given number. i.e. num1. It is a static function of java math class and can be accessed using the math class.
Syntax:
Math.sqrt(number);
Definition of Parameters:
numberinput on which operation is Performed.
Return Type :
If the input is + double value, sqrt will return the square root of a given value.
If the input is NaN or –, sqrt method will return NaN.
If the input is +infinity, sqrt method will return +Infinity.
import java.util.*;
public class Main{
public static void main(String[] args){
// NaN value
double num1 = Double.NaN;
// resultant value for NaN value
System.out.println("Resultant value : " + Math.sqrt(num1));
// zero as argument
double num = 0.0;
System.out.println("Zero as Argument : " + Math.sqrt(num));
// infinity as argument
num = Double.POSITIVE_INFINITY;
System.out.println("Infinity as Argument : " + Math.sqrt(num));
}
}
Output:
Resultant value : NaN
Zero as Argument : 0.0
Infinity as Argument : Infinity
In the above Example, We had taken a NaN value, 0 value and positive infinity as input for the sqrt function. The sqrt method returns the result of the given inputs respectively.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment