Java Program to Find Square Root of a Number

Java program to find the square root of a number using the sqrt() method of java.lang.Math class. We will also develop a java program to find the square root of a number without the sqrt method.

To find the square root of a number sqrt() method is given in the java.lang.Math class. The prototype of the sqrt() method is:-

public static double sqrt(double a)

Parameter type:- double
Return type:- double
Return value:- the positive square root of argument.

It returns the correctly rounded positive square root of a double value. Special cases for return values:

=> If the argument is NaN or less than zero, then the result is NaN (Not a number).
=> 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.

Java program to find the square root of a number using sqrt() method

import java.util.Scanner;

public class SquareRoot {

   public static void main(String[] args) {

      // declare variables
      double number = 0;
      double squareRoot = 0;

      // read input from console
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a number: ");
      number = scan.nextDouble();

      // find sqrt() value
      squareRoot = Math.sqrt(number);

      // display result
      System.out.println("Square root "
		+ "value = "+ squareRoot);

      // close Scanner class object
      scan.close();
   }
}

Output for the different test-cases:-

Enter a number: 25
Square root value = 5.0

Enter a number: 10
Square root value = 3.1622776601683795

You can format the output using modifiers,

System.out.printf("Square root value = %.2f"
                             , squareRoot);

Enter a number: 10
Square root value = 3.16

See more:- Output formatting using printf()

If you want to convert the double value to integer value then you can use type-casting.

int squareRoot = (int) Math.sqrt(number);
System.out.println("Square root value = "
                          + squareRoot);

Now, the output will be

Enter a number: 10
Square root value = 3

Find the square root of a number without the sqrt method

Procedure to develop method to find square root value

1) Take a iterator variable and initialize it with 1.
2) Check the given number is perfect square? If i*i = n then i is the square root value of n.
3) Else find the smallest value of i for whcih i*i is strictly greater than n. The square root lies in the interval i-1 to i. Now, binary search algorithm can be used to find the square root value upto n decimal places.
4) Increase i value by 1

Binary search algorithm to find square root value up to n decimal place

1) Find the middle of (i-1) and i.
2) If middle * middle = n then the middle is the square root value. Or, compare middle * middle with n, with precision up to n decimal places if their difference is very less then also middle is the square root value.
3) Else if middle * middle < n then square root belongs to the second half
4) Else if middle * middle < n then square root belongs to the first half

import java.util.Scanner;

public class SquareRoot {

   // method to find square root value of a number
   private static double squareRoot(double number) {

      int i = 1; // iterator variable

      while(true) {

         // for perfect square
         if(i*i == number)
         return i;

         // for others
         else if(i*i > number) 
         return sqrtInDecimal(number,i-1,i);

         // increase i value by 1
         i++;
      }
   }

   // recursive method to find square root value  
   // up to 7 decimal places
   private static double sqrtInDecimal(double number, 
                                double i, double j) {

      // find middle of i and j
      double middle = (i+j)/2;

      // find square value of middle
      double sqr = middle * middle;

      // check middle is square root or not
      if(sqr==number || 
      	    Math.abs(sqr-number)<0.0000001) 
      return middle; 

      // when square root belongs to second half
      else if(sqr > number)
      return sqrtInDecimal(number, i, middle);

      // when square root belongs to first half
      else
      return sqrtInDecimal(number, middle, j);
   }

   public static void main(String[] args) {

      // declare variables
      double number = 0;
      double sqrtValue = 0;

      // read input from console
      Scanner scan = new Scanner(System.in);
      System.out.print("Enter a number: ");
      number = scan.nextDouble();

      // find sqrt() value
      sqrtValue = squareRoot(number);

      // display result
      System.out.println("Square root "
		+ "value = "+ sqrtValue);

      // close Scanner class object
      scan.close();
   }
}

Output for the different test-cases:-

Enter a number: 25
Square root value = 5.0

Enter a number: 5
Square root value = 2.236067980527878

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *