Answer:

Since negative numbers do not have a real square root, the loop would be unable to compute it and would run forever.

Checking the User Input

There are several ways in which defective user input could be handled. One way is to check the value that the user entered. If it is OK, execute the loop. Otherwise, write an error message to the screen and end the program. Here is the revised program, with some blanks:

import java.util.Scanner;
class  SquareRoot
{
  public static void main( String[] args ) 
  {
    final double smallValue = 1.0E-14 ;
    double N  ;             // the user enters N
    double guess = 1.00 ;   // the same first guess works for any N

    // get the number from the user
    Scanner scan = new Scanner( System.in );
    System.out.print("Enter the number: "); 
    N = scan.nextDouble();

    
    {
      while ( Math.abs( N/(guess*guess) - 1.0 ) > smallValue )
      {      
         guess =  N/(2*guess) + guess/2 ; // calculate a new value for the guess
      }

      System.out.println("The square root of " + N + " is " + guess ) ;
    }
    
    {
      ;
    }

  }

}

QUESTION 20:

Fill in the blanks so that the program checks that the user input is positive and writes an error message if not.