/**
* Square root using Newton Raphson's methods
* @author PRATEEK
*/
class NewtonRaphsonSquareRoot {
public static void squareRoot(float num)
{
float x1 = num,x2=0,temp=num,e=0.001f;
do
{
x1 = temp;
x2 = 0.5f * (x1 + num/x1);
temp=x2;
}while(Math.
abs(x2
-x1
)> e
);
System.
out.
println(num
+ " ----> " + x2
); }
public static void main
(String[] args
) { squareRoot(44.89f);
squareRoot(2f);
squareRoot(64f);
}
}
LyoqCiAqIFNxdWFyZSByb290IHVzaW5nIE5ld3RvbiBSYXBoc29uJ3MgbWV0aG9kcwogKiBAYXV0aG9yIFBSQVRFRUsKICovCiBjbGFzcyBOZXd0b25SYXBoc29uU3F1YXJlUm9vdCB7CgoJcHVibGljIHN0YXRpYyB2b2lkIHNxdWFyZVJvb3QoZmxvYXQgbnVtKQoJewoJCWZsb2F0IHgxID0gbnVtLHgyPTAsdGVtcD1udW0sZT0wLjAwMWY7CgkJZG8gCgkJewoJCQl4MSA9IHRlbXA7CgkJCXgyID0gMC41ZiAqICh4MSArIG51bS94MSk7CgkJCXRlbXA9eDI7CgkJCQoJCX13aGlsZShNYXRoLmFicyh4Mi14MSk+IGUpOwoJCQoJCVN5c3RlbS5vdXQucHJpbnRsbihudW0gKyAiIC0tLS0+ICIgKyB4Mik7Cgl9CgkKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCQlzcXVhcmVSb290KDQ0Ljg5Zik7CgkJc3F1YXJlUm9vdCgyZik7CgkJc3F1YXJlUm9vdCg2NGYpOwoJfSAKfQ==