2

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.
     */

    }
}
4
  • The value for calculation is calculated outside of the loop. So it will never change. You need to put it inside the loop Commented Jun 28, 2017 at 19:26
  • 1
    But declare it before the loop so it doesn't get overwritten every time. Commented Jun 28, 2017 at 19:27
  • If you want to calculate multiple times, you will have to run the code multiple times, in the loop. Now you just print out the same value (calculation) over and over again. So you either write a function calculation() that is called repeatedly, or you put it in the loop. Note that the formula must be repeated several times before you get a proper estimate of the square root. Commented Jun 28, 2017 at 19:29
  • To put it another way - all the current loop does is print the value of calculation over and over. There is no logic or calculation happening repeatedly - it is not calculating the square root multiple times. Commented Jun 28, 2017 at 19:29

2 Answers 2

2

Instead of

int calculation = ((x/2) + (x/(x/2)) / 2);

for(int i = 0; i < n; i++) {
    System.out.println((double)calculation);
}

use

for(int i = 0; i < n; i++) {
    x = ((x/2) + (x/(x/2)) / 2);
    System.out.println((double) x );
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thank you!
2

Change it to this:

double calculation = x;

for(int i = 0; i < n; i++) {
    calculation = ((calculation/2) + (calculation/(calculation/2)) / 2)
    System.out.println(calculation);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.