PROGRAM avg2

IMPLICIT NONE 

! A program to average an arbitrarily long list of numbers.

! This version uses std in / std out for i/o
!   but now we also use iostat keyword

INTEGER n, ierr
REAL x, avg

avg = 0.
n = 0

DO 
   WRITE(*,*) 'enter a number to average'
   READ(*,*,IOSTAT=ierr) x
   IF(ierr /= 0) EXIT
   avg = avg + x
   n = n + 1
ENDDO

WRITE(*,*)'the average is : ',avg / n

END PROGRAM avg2
