! Program Example from "Essential Fortran 90 & 95" by Loren P. Meissner
! Copyright 1996. Copying for sale requires permission from the author.
! Otherwise, distribution is permitted if these three lines are included.

! Example 4.10. Square root by Newton's method.
  program E10
    implicit none
    integer, parameter :: K = selected_real_kind( 12 )
    real(kind = K) :: X, Root, Previous
    real(kind = K), parameter :: TOLERANCE = 1.0e-10_K
! start program E10
    write (unit = *, fmt = *) " Please enter the number whose square root is wanted. "
    read (unit = *, fmt = *) X
    X = abs( X )  ! This algorithm requires that X be nonnegative.
    Root = 1.0_K  ! Use 1.0 as first guess for Root.
    do
      Previous = Root
      Root = 0.5_K * (Root + X / Root)
      if (abs( Root - Previous ) <= TOLERANCE) then
        exit
      end if
      write (unit = *, fmt = *) Root
    end do
    write (unit = *, fmt = *) " Square root of: ", X, " is: ", Root
    stop
  end program E10
