×

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

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java Enum vs Class

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four integrators named Club, Diamond,...

7 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

POJO in Java

Plain old Java Object, in short, is called POJO in Java. POJO is an everyday object that is not subject to any specific limitations. We can use POJO in any Java...

3 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

String Declaration in Java

A string is a group of characters. In Java, the string can be treated as both class and datatype. In Java programming, the String class have many advantages. Everything in...

3 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

11 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

How to Set Java_home in Linux

To set the Java_home in Linux, we must follow several steps to make us understand it easily.  Java follows the principle called WORA (write once run anywhere). We know that java...

3 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Fibodiv Number in Java

Before understanding the concept of the fibodiv number, one should realize what the Fibonacci number is. What are Fibonacci Numbers? The Fibonacci sequence of whole numbers is composed of the numbers 0,...

5 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.