Java Program to Find Square Root of a Number Without sqrt Method

In Java, to find the square root of a number is very easy if we are using the pre-defined method. Java Math class provides sqrt() method to find the square root of a number. In this section, we will create a Java program to find the square root of a number without using the sqrt() method. It is the most popular question asked in the Java interview.

If the square of a number is x, the square root of that number will be the number multiplied by itself. For example, the square root of 625 is 25. If we multiply 25 two times, we get the square of the number. Mathematically, the square root of a number is given as:x=√x

We have used the following formula to find the square root of a number.sqrtn+1=(sqrtn+(num/sqrtn))/2.0

Note: The first sqrt number should be the input number/2.

Let’s implement the above formula in a Java program and find the square root.

FindSquareRootExample1 .java

  1. import java.util.Scanner;  
  2. public class FindSquareRootExample1   
  3. {  
  4. public static void main(String[] args)    
  5. {   
  6. System.out.print(“Enter a number: “);  
  7. //creating object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. //reading a number form the user  
  10. int n = sc.nextInt();  
  11. //calling the method and prints the result  
  12. System.out.println(“The square root of “+ n+ ” is: “+squareRoot(n));  
  13. }  
  14. //user-defined method that contains the logic to find the square root  
  15. public static double squareRoot(int num)   
  16. {  
  17. //temporary variable  
  18. double t;  
  19. double sqrtroot=num/2;  
  20. do   
  21. {  
  22. t=sqrtroot;  
  23. sqrtroot=(t+(num/t))/2;  
  24. }   
  25. while((t-sqrtroot)!= 0);  
  26. return sqrtroot;  
  27. }  
  28. }  

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

Let’s see another logic to find the square root.

In the following example, we have used the following procedure to find the square root.

  • We have initialized an iterator variable i=1.
  • Check the number that we have entered is a perfect square or not. If the square of i is equal to n, i will be the square root value of n.
  • Otherwise, find the lowest value of i. Remember that the square of i must be greater than n. The square root of a number lies between i-1 and i. After performing the steps, we use the binary search algorithm to find the square root of a number up to n decimal places.
  • Increment the variable i by 1.

Binary Search Algorithm

  • Find the midvalue of i-1 and i.
  • Find the square of midvalue and compare it with n.
    • If midvalue * midvalue = n, the midvalue is the square root of the given number. Compare the square of midvalue with n (up to n decimal places) if the difference is minor, the midvalue will be the square root of the number.
    • If midvalue * midvalue > n, the square root belongs to the first half.
    • If midvalue * midvalue < n, the square root belongs to the second half.

Let’s implement the algorithm in a Java program and find the square root of a number.

FindSquareRootExample2.java

  1. import java.util.Scanner;  
  2. public class FindSquareRootExample2   
  3. {  
  4. public static void main(String[] args)   
  5. {  
  6. double number = 0, sqrt=0;  
  7. //object of the Scanner class  
  8. Scanner sc = new Scanner(System.in);  
  9. System.out.print(“Enter a number: “);  
  10. //reading a double value from the user  
  11. number = sc.nextDouble();  
  12. //method calling  
  13. sqrt = squareRoot(number);  
  14. //prints the result  
  15. System.out.println(The square root of “+number+ ” is ” +sqrt);  
  16. }  
  17. //user-defined method to find the square root of a number  
  18. private static double squareRoot(double number)   
  19. {  
  20. //iterator variable      
  21. int i = 1;  
  22. while(true)   
  23. {  
  24. //for perfect square numbers  
  25. if(i*i == number)  
  26. return i;  
  27. //for not perfect square numbers  
  28. else if(i*i > number)   
  29. //returns the value calculated by the method decimalSqrt()  
  30. return decimalSqrt(number,i-1,i);  
  31. //increments the variable i by 1  
  32. i++;  
  33. }  
  34. }  
  35. // recursive method to find the square root of a number up to 7 decimal places    
  36. private static double decimalSqrt(double number, double i, double j)   
  37. {  
  38. //calculates the middle of i and j  
  39. double midvalue = (i+j)/2;  
  40. //finds the square of the midvalue  
  41. double square = midvalue * midvalue;  
  42. //compares the midvalue with square up to n decimal places  
  43. if(square==number||Math.abs(square-number)<0.0000001)   
  44. return midvalue;   
  45. //if the square root belongs to second half  
  46. else if(square>number)  
  47. return decimalSqrt(number, i, midvalue);  
  48. //if the square root belongs to first half  
  49. else  
  50. return decimalSqrt(number, midvalue, j);  
  51. }  
  52. }  

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

Leave a Comment