Ask Question
6 April, 16:31

Let's say you are given a number, a, and you want to find its square root. One way to do that is to start with a very rough guess about the answer, x0, and then improve the guess using the following formula: x1 = (x0 + a/x0) / 2 For example, if we want to find the square root of 9, and we start with x0 = 6, then x1 = (6 + 9/6) / 2 = 15/4 = 3.75, which is closer. We can repeat the procedure, using x1 to calculate x2, and so on. In this case, x2 = 3.075 and x3 = 3.00091. So that is converging very quickly on the right answer (which is 3). Write a method called squareRoot that takes a double as a parameter and that returns an approximation of the square root of the parameter, using this technique. You may not use Math. sqrt. In java

+3
Answers (1)
  1. 6 April, 17:57
    0
    Check the explanation

    Explanation:

    package com. squarerrot;

    public class SquareRoot {

    /**

    *

    */

    public static double squareRoot (double input) {

    System. out. println ("Trace for input:" + input);

    //random value initialized to 6 here

    double output, guessValue = 6, previousValue = 0;

    while (input - (output = approximateValue (input, guessValue)) > = 0.00005

    && previousValue! = output) {

    guessValue = output;

    previousValue = output;

    System. out. println (output);

    }

    return output;

    }/ / end of method squareRoot

    / / calculate the approximate value

    public static double approximateValue (double input, double start) {

    return (start + (input / start)) / 2;

    }/ / end of method approximateValue

    / / test the square root method

    public static void main (String[] args) {

    System. out. println ("Square Root of 9:" + squareRoot (9));

    System. out. println ("Square Root of 16:" + squareRoot (16));

    }/ / end of method main

    }/ / end of the class
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Let's say you are given a number, a, and you want to find its square root. One way to do that is to start with a very rough guess about the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers