Find Square Root of a Number Without sqrt Method in Java

In Java, calculating the square root of a number becomes simple when using the built-in method. The Math class in Java offers the sqrt() method specifically for this purpose. However, in this section, we will focus on creating a Java program that determines the square root of a number without utilizing the sqrt() method. This particular approach is a common question asked during Java interviews.

If we have a number x and its square is denoted as x^2, then the square root of x can be obtained by multiplying the number by itself. For instance, the square root of 625 is 25 because when we multiply 25 by itself, we obtain the square of the number. Mathematically, the square root of a number is defined as follows:

x=√x

The following formula is used to find the square root of a number.

sqrtn+1=(sqrtn+(num/sqrtn))/2.0

FindSquareRootExample1.java

import java.util.Scanner;  
public class FindSquareRootExample   
{  
public static void main(String[] args)    
{   
System.out.print("Enter a number: ");  
//creating object of the Scanner class  
Scanner sc = new Scanner(System.in);  
//reading a number form the user  
int n = sc.nextInt();  
//calling the method and prints the result  
System.out.println("The square root of "+ n+ " is: "+squareRoot(n));  
}  
//user-defined method that contains the logic to find the square root  
public static double squareRoot(int num)   
{  
//temporary variable  
double t;  
double sqrtroot=num/2;  
do   
{  
t=sqrtroot;  
sqrtroot=(t+(num/t))/2;  
}   
while((t-sqrtroot)!= 0);  
return sqrtroot;  
}  
}

Output 1:

Enter a number: 12
The square root of 12 is: 3.4641016151377544

Output 2:

Enter a number: 25
The square root of 25 is: 5.0

In the following example, an alternative approach is employed to calculate the square root of a number.

The procedure used is as follows:

  1. Initialize an iterator variable, i, to 1.
  2. Check whether the given number is a perfect square or not. If the square of i is equal to the given number, then i is the square root of the number.
  3. If the number is not a perfect square, determine the lowest value of i where the square of i is greater than the given number. The square root of a number lies between i-1 and i.
  4. To find the square root up to a specified number of decimal places, the binary search algorithm is utilized.
  5. Increment the value of i by 1.

Binary Search Algorithm

To calculate the square root of a number using the given approach:

  1. Determine the midvalue between i-1 and i.
  2. Square the midvalue and compare it with the given number, n.
  3. If the square of the midvalue is equal to n, then the midvalue is the square root of the given number. Additionally, compare the square of the midvalue with n, considering the decimal places. If the difference is minimal, the midvalue can be considered as the square root of the number.
  4. If the square of the midvalue is greater than n, it indicates that the square root belongs to the first half of the range.
  5. Conversely, if the square of the midvalue is less than n, the square root belongs to the second half of the range.

FindSquareRootExample2.java

import java.util.Scanner;  
public class FindSquareRootExample1   
{  
public static void main(String[] args)   
{  
double number = 0, sqrt=0;  
//object of the Scanner class  
Scanner sc = new Scanner(System.in);  
System.out.print("Enter a number: ");  
//reading a double value from the user  
number = sc.nextDouble();  
//method calling  
sqrt = squareRoot(number);  
//prints the result  
System.out.println(The square root of "+number+ " is " +sqrt);  
}  
//user-defined method to find the square root of a number  
private static double squareRoot(double number)   
{  
//iterator variable      
int i = 1;  
while(true)   
{  
//for perfect square numbers  
if(i*i == number)  
return i;  
//for not perfect square numbers  
else if(i*i > number)   
//returns the value calculated by the method decimalSqrt()  
return decimalSqrt(number,i-1,i);  
//increments the variable i by 1  
i++;  
}  
}  
// recursive method to find the square root of a number up to 7 decimal places    
private static double decimalSqrt(double number, double i, double j)   
{  
//calculates the middle of i and j  
double midvalue = (i+j)/2;  
//finds the square of the midvalue  
double square = midvalue * midvalue;  
//compares the midvalue with square up to n decimal places  
if(square==number||Math.abs(square-number)<0.0000001)   
return midvalue;   
//if the square root belongs to second half  
else if(square>number)  
return decimalSqrt(number, i, midvalue);  
//if the square root belongs to first half  
else  
return decimalSqrt(number, midvalue, j);  
}  
}

Output 1:

Enter a number: 625
The square root of 625.0 is 25.0

Output 2:

Enter a number: 129
The square root of 129.0 is 11.357816688716412

To gain further knowledge on the topic of ‘Find Square Root of a Number Without sqrt Method,’ we recommend following the tutorials available on the platform tutorials.freshersnow.com.