! 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.6. Sum alternating harmonic series until term is small.
  program E06
    implicit none
    integer, parameter :: K = selected_real_kind( 12 )
    real(kind = K) :: Total, Term_Sign, Next_Term
    integer :: N
    real(kind = K), parameter :: TOLERANCE = 1.0e-4
! start program E06
    Total = 0.0_K
    Term_Sign = 1.0_K
    N = 1
    do ! forever
      Next_Term = Term_Sign / real( N, kind = K )
      if (abs( Next_Term ) < TOLERANCE) then
        exit
      end if
      Total = Total + Next_Term
      N = N + 1
      Term_Sign = - Term_Sign
    end do
    write (unit = *, fmt = *) " The sum after: ", N - 1, " terms is: ", Total
    stop
  end program E06
