Home Tutorial Java Core Java : Square root of any number

 
 

Java : Square root of any number
Posted on: November 23, 2012 at 12:00 AM
In this section we will write code for finding square root of any number without using system methods.

Java : Square root of any number

In this section we will write code for finding square root of any number without using system methods.

Square root : It is number when we square it, find the specified number. There is simplest way to find out the square root of any number by calling math.sqrt() method. You can also write your logic to find it.

Example : In this example we are showing how to find a square root of any number without using math.sqrt() method.

class FindSquareRoot {
	public static void main(String[] args) {
		double n = 20;
		double x = 1;
		for (int i = 0; i < n; i++) {
			x = 0.5 * (x + n / x);
		}

		System.out.println("Square root of " + n + " is " + x);
		}
	}	

Output :

Square root of 20.0 is 4.47213595499958

Related Tags for Java : Square root of any number :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.