Home Tutorial Java Core Java : Square root of any number

 
 

Share on Google+Share on Google+
Java : Square root of any number
Posted on: November 23, 2012 at 12:00 AM
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
Advertisement

Related Tags for Java : Square root of any number :


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.

Posted on: November 23, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com