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

Java8Java Programming Object Oriented Programming

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
Published on 26-Apr-2018 12:59:44