In: Computer Science
Java please. Write a static method sqrt()that takes a double argument and returns the square root of that number using newton's method to compute result.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
class A {
static double sqrt(double n) {
double l = 2;
double x = n;
double root;
int count = 0;
while (true) {
count++;
root = 0.5 * (x + (n / x));
if (Math.abs(root - x) < l)
break;
x = root;
}
return root;
}
public static void main(String[] args) {
double n = 327;
System.out.println(sqrt(n));
}
}