Program to Find Square Root of a Number Without sqrt Method in Java
The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's square root without utilizing the sqrt ( ) function.
A number’s square root is multiplied by itself if the square of the number is x. For instance, 5 is the square root of 25. The square root of 25 is obtained by multiplying 5 by twice.
Java Program to find square root of a number
SquareRoot.java
// importing util package for scanner class
import java . util . * ;
public class SquareRoot
{
public static void main ( String [ ] s )
{
System . out. println ( " Enter the number: " ) ;
// Using the scanner class, we can take inputs from the user during the run Runtime
// creating an Object for the class Scanner
Scanner scan = new Scanner ( System . in ) ;
// Taking number form the user and storing it to the integer variable n
int n = scan . nextInt ( ) ;
System . out. println ( " The Square root of " + n + " is : " + Root ( n ) ) ;
}
/ /user-defined method to find the square root
public static double Root ( int n )
{
// creating a temporary table variable
double t ;
// creating double variable
double ans = n/2 ;
do
{
t = ans ;
ans = ( t + ( n/t ) ) / 2 ;
}
while ( ( t - ans ) != 0 ) ;
// returning the answer
return ans;
}
}
Output
Enter the number :
25
The Square root of 25 is: 5.0
Here is another approach to determining the square root.
Procedure
- Set the iterator variable i to be equal to 1.
- Verify whether the number we entered is a perfect square. Variable I, will be equal to the value of n's square root if the square of i equals n.
- Otherwise, locate its lowest value. Keep in mind that its square must be larger than n. A number's square root is present between i-1 and i. Following these procedures, we employ the binary search technique to determine a number's square root up to n decimal places.
- Add one to the variable i.
Algorithm for Binary Search
- Find the midpoint between I and i-1.
- Find the mid-value square, then compare it to n.
- The mid value is defined as the square root of the given number mid value * mid value = n. If there is a slight difference between the square of the mid-value and n, the mid-value is the square root of the given number.The square root is a member of the mid-first value's half, and the mid-value is greater than n.
- The second part of mid-value * mid-value n contains the square root.
Using Binary Search
public class Main
{
public static void main(String[] s)
{
double n = 0, ans=0;
Scanner sc = new Scanner ( System . in ) ;
System.out.println ( " Enter a number: " ) ;
n = sc . nextDouble ( ) ;
ans = squareRoot( n ) ;
System . out . println ( " The square root of "+ n +" is " + ans ) ;
}
private static double squareRoot ( double number )
{
int j = 1;
while(true)
{
if ( j * j == number )
return j ;
else if ( j * j > number )
return Sqrt( number , j-1 ,j ) ;
j++;
}
}
private static double Sqrt ( double number , double i , double j )
{
double midvalue = ( i + j ) / 2 ;
double square = midvalue * midvalue ;
if ( square = = number | | Math . abs ( square - number ) < 0.0000001 )
return midvalue ;
else if ( square > number )
return Sqrt ( number, i, midvalue ) ;
else
return Sqrt ( number, midvalue , j ) ;
}
}
Output
Enter a number: 8
The square root of 8.0 is 2.8284271359443665