! 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.14. Average groups of real Numbers.
  program E14
  implicit none
  real :: Next, Total, Average
  integer :: How_Many
! start program E14
    write (unit = *, fmt = *) " Please enter a group of numbers to be averaged. "
    write (unit = *, fmt = *) " Enter zero to end the group. "
    write (unit = *, fmt = *) " To quit, enter zero as the first number in a group. "
    do
      How_Many = 0
      Total = 0.0
      do
        write (unit = *, fmt = *) " Next number: "
        read (unit = *, fmt = *) Next
        if (Next == 0.0) then
          exit  ! Exit from inner loop.
        end if
        How_Many = How_Many + 1
        Total = Total + Next
      end do
      if (How_Many == 0) then
        exit  ! Exit from outer loop.
      end if
      Average = Total / real( How_Many )
      write (unit = *, fmt = *) " The average of ", How_Many, " Numbers is: ", Average
    end do
    stop
  end program E14
