import java.io.*; public class sqrt { public static void main(String[] argv) { double x, sqrt, epsilon; x = 3; epsilon = 0.005; System.out.println(" square root of " + x + " is " + squareRoot(x,epsilon)); } // find square root of x with the given precision private static double squareRoot(double x, double epsilon) { double y; y = x; while (!closeEnough(x, y*y, epsilon)) y = (y + x / y) / 2.0; return y; } // squareRoot // compare the tolerance private static boolean closeEnough(double a, double b, double eps) { return Math.abs(a - b) < eps; } // closeEnough }