×

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

Related Topics

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Star Pattern Programs in Java

Star Pattern Programs in Java The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier. Right Triangle Star Pattern Filename: StarPatternExample.java public class StarPatternExample {              public static void...

4 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

Least Operator to Express Number in Java

In this article, we will learn about how to obtain a target number using a single number or a single integer by leveraging least operators in Java. There can be...

3 minutes read.

java.lang.NumberFormatException for Input String

java.lang.NumberFormatException for Input String The exception java.lang.NumberFormatException for input string occurs when we try to convert a string into a number format. For example, if someone converts the string “Tutorial & Example”...

3 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.