how can I use a counter controlled iteration in the print/math section on this square root program

Viewed 48

I was thinking about adding a while loop to the program that'll loop for maybe 10 times instead of writing 10 println statements. but I found it hard since the value of x is different every time in the math section. I wrote this code before, now I want to shorten it. It's a square root finder program that uses the Babylonian method to find the square root of an integer between [S > 20 || S < 400]

    int S;
    System.out.print("Enter an integer, S: ");
    S = myInput.nextInt();

    if (S < 0) {
        System.out.println("This program can not take the square root of a negative number.");

    }

    else if (S < 20 || S > 400) {
        System.out.println("This value is out of range.");
    }

    else {
        double a = S / 2.0;
        double b = S / a;
        double c = a + b;
        double d = 0.5 * c;
        // for x2
        double e = S / d;
        double f = d + e;
        double g = 0.5 * f;
        // for x3
        double h = S / g;
        double i = g + h;
        double j = 0.5 * i;
        // for x4
        double k = S / j;
        double l = j + k;
        double m = 0.5 * l;
        // for x5
        double n = S / m;
        double o = m + n;
        double p = 0.5 * o;
        // for x6
        double q = S / p;
        double r = p + q;
        double s = 0.5 * r;
        // for x7
        double t = S / s;
        double u = s + t;
        double v = 0.5 * u;
        // for x8
        double w = S / v;
        double x = v + w;
        double y = 0.5 * x;
        // for x9
        double z = S / y;
        double aa = y + z;
        double ab = 0.5 * aa;

        System.out.printf("%nx0 = %8.4f ", a);
        System.out.printf("%nx1 = %8.4f ", d);
        System.out.printf("%nx2 = %8.4f ", g);
        System.out.printf("%nx3 = %8.4f ", j);
        System.out.printf("%nx4 = %8.4f ", m);
        System.out.printf("%nx5 = %8.4f ", p);
        System.out.printf("%nx6 = %8.4f ", s);
        System.out.printf("%nx7 = %8.4f ", v);
        System.out.printf("%nx8 = %8.4f ", y);
        System.out.printf("%nx9 = %8.4f ", ab);

    }
1 Answers

All you need to do is set d to 2.0 outside the loop. Then use d in place of 2.0 inside the loop. The loop index of i is also used to name the iterations (x0, x1, x2, ...) when printing.

double d = 2.0;        // set d to 2.0 here
for (int i = 0; i < 10; i++) {
    double a = S / d;  // use d here, the modified value will be used again
    double b = S / a;
    double c = a + b;
    d = 0.5 * c;
    System.out.printf("x%d = %8.4f%n", i, a);
}

prints the following for the input value of 50

x0 =  25.0000
x1 =   3.7037
x2 =   5.8127
x3 =   6.9374
x4 =   7.0698
x5 =   7.0711
x6 =   7.0711
x7 =   7.0711
x8 =   7.0711
x9 =   7.0711

Here is an exercise. Compare the current computed value to the last. If they are equal (or their difference is small), then you can exit the loop since repeated iterations won't improve the accuracy very much.

Related