# Java program to find the square root of a given number

Java8Java Programming Object Oriented Programming

#### Complete Java Programming Fundamentals With Sample Projects

98 Lectures 7.5 hours

#### Get your Java dream job! Beginners interview preparation

85 Lectures 6 hours

#### Core Java bootcamp program with Hands on practice

Featured

99 Lectures 17 hours

The process of finding the square root of a number can be divided into two steps. One step is to find integer part and the second one is for fraction part.

## Algorithm

• Define value n to find the square root of.
• Define variable i and set it to 1. (For integer part)
• Define variable p and set it to 0.00001. (For fraction part)
• While i*i is less than n, increment i.
• Step 4 should produce the integer part so far.
• While i*i is less than n, add p to i.
• Now i have the square root value of n.

## Example

Live Demo

public class SquareRoot {
public static void main(String args[]){
int n = 24;
double i, precision = 0.00001;
for(i = 1; i*i <=n; ++i);
for(--i; i*i < n; i += precision);
System.out.println("Square root of given number "+i);
}
}

## Output

Square root of given number 4.898979999965967