Java : Square root of any number


 

Java : Square root of any number

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

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

Ads