I have an assignment that requires me to square root a number as many times as I want. The console asks the number I want to square root and how many times I want it to. My code square roots the number multiple times, but it gives the same value. How can I make the value get closer to the number's square root?
import java.util.*;
public class SquareRootCalculator {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int x; // the number whose root we wish to find
int n; // the number of times to improve the guess
// Read the values from the user.
System.out.print("input a positive integer (x): ");
x = scan.nextInt();
System.out.print("number of times to improve the estimate: ");
n = scan.nextInt();
int calculation = ((x/2) + (x/(x/2)) / 2);
for(int i = 0; i < n; i++) {
System.out.println((double)calculation);
}
/*
* The lines above read the necessary values from the user
* and store them in the variables declared above.
* Fill in the rest of the program below, using those
* variables to compute and print the values mentioned
* in the assignment.
*/
}
}