Share on Google+Share on Google+

Java : Square root of any number

Advertisement
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

Advertisements

Advertisement

Posted on: November 23, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.