Java Math sqrt() Function

pow() Function in Java

Java Math Class

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:

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.
  • If the given is Zero, sqrt method will return 0.

Example :

Run
import java.util.*;

public class Main{
  public static void main(String[] args) {
    // Initializing Double argument
    double num1 = 9;
    
    // Storing the resultant
    double ans = Math.sqrt(num1);
    
    // Printing the square root
    System.out.println("Resultant Value : " + ans);
  }
}
Output:

Resultant Value : 3.0

In the above Example, We had taken a double value. The sqrt method returns the square root of the given input i.e. num2.

Example for Different Data Values:

Run
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

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription