Understanding the sqrt() Method in Java: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-04 04:12:00   6640  Share
Subject Syllabus DetailsSubject Details 1 Program
☰ TContent
☰Fullscreen

Table of Content:

Description

This java tutorial shows how to use the sqrt(double a) method of Math class under java.lang package. 

The method returns the square root of the argument.

Syntax

double sqrt(double d)

Parameters

Here is the detail of parameters ?

  • d ? Any primitive data type.

Return Value

  • This method returns the square root of the argument.

The sqrt(double a) method simply returns the positive square root of a. Consider the following 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.

Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

Compatibility Version :

Requires Java 1.0 and up

Example

public class MathSqrt {

   public static void main(String args[]) {
      double x = 11.635;
      double y = 2.76;

      System.out.printf("The value of e is %.4f%n", Math.E);
      System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));
   }
}

Output

This will produce the following result ?

The value of e is 2.7183
sqrt(11.635) is 3.411
Press any key to continue . . .

Example

This java example source code demonstrates the use of sqrt(double a) method of Math class. Basically we just get the square root of a value.


import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * sqrt(double a) method of Math class
 * Get the square root value of the user input
 */

public class MathSquareRoot {

	public static void main(String[] args) {
		// ask for user input
		out.print("Enter a value:");
		Scanner scan = new Scanner(System.in);
		// use scanner to get user console input
		double value = scan.nextDouble();
		// get the square root of a value
		double sqrtValue = Math.sqrt(value);
		out.println("square root of "+value+" = "+sqrtValue);
		// close the scanner object to avoid memory leak
		scan.close();

	}

}

Output

Running the sqrt(double a) method example source code of Math class will give you the following output.

Enter a value:81
square root of 81.0 = 9.0
Press any key to continue . . .

Example

This java example source code demonstrates the use of sqrt(double a) method of Math class. Basically we just get the square root of a value.

/*
  Find square root of a number using Math.sqrt
  This java example shows how to find square root of a given number using
  sqrt method of Java Math class.
*/
public class FindSquareRootExample {

  public static void main(String[] args) {

    /*
     * To find square root value of a number, use
     * static double sqrt(double d1) method Java Math class.
     */

     //returns square root of 9, i.e. 3
     System.out.println(Math.sqrt(9));

     //returns square root of 25.5
     System.out.println(Math.sqrt(25.5));
  }
}

Output

Running the sqrt(double a) method example source code of Math class will give you the following output.

3.0
5.049752469181039
Press any key to continue . . .