Fortran: Lesson 7

Lesson topics

View Demos
#1 #2 #3
Download Demos
#1 #2 #3

Demo 3

								        program addthem
c       This program reads a list of numbers from a file named
c       "numbers", and computes the sum of the numbers.
c       Because each read statement reads only one number,
c       each number in the file must appear on its own line.
c       The "go to" loop terminates when the end of the file is
c       reached - the program then prints the sum and ends.
c       As no drive letter is given, the file "numbers" must
c       reside in the directory from where Fortran is running.
        real x, sum
        open (unit=1,file="numbers")
        sum = 0
 5      read (1,*,end=10) x
        sum = sum + x
        go to 5
 10     print *
        print *, "The sum of the numbers in the file is", sum
        print *, "Bye"