! 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.9. Sum a power series until next term is small enough.
  program E09
    implicit none
    integer, parameter :: K = selected_real_kind( 12 )
    real(kind = K) :: Total, Next_Term
    real(kind = K), parameter :: TOLERANCE = 1.0e-6_K
! start program E09
    Total = 0.0_K
    Next_Term = 1.0_K
    do
      if (Next_Term < TOLERANCE) then
        exit
      end if
      Total = Total + Next_Term
      Next_Term = 0.5_K * Next_Term
    end do
    write (unit = *, fmt = *) " Sum of terms greater than: ", real(TOLERANCE), " is: ", Total
    stop
  end program E09
