11 Sample FORTRAN Programs

by 27-150 Student[name lost]


C Program #1
C This program prints words to the screen

        print *,'Hello World'
        end

C Program #2
C This program inputs a number and then prints it

C First declare the variable, the thing you are going to input
        integer number

C now input the number from the keyboard
        print *,'Enter an integer'
        read *,number

C now print the number that was inputed
        print *,'The number you typed was...'
        print *,number
        end


C Program #3
C This program inputs a name and then prints it

C First declare the name as a character
        character*20 name

C Now get the input from the keyboard
        print *,'Enter your name;'
        read 41,name
41      format (a20)

        print *,'Your name is ',name
        end



C Program #4
C This program inputs a variable and then assigns that number to
C a new variable and then prints the result.

C First declare the two variables
        integer var1, var2

C now input the number from the keyboard
        print *,'enter an integer'
        read *,var1

        var2 = var 1

        print*,'Now printing the value of var2:',var2
        end


C Program #5
C This program takes you number and adds to it several times

C First declare the variable
        integer i

C Input the variable
        print*,'Enter an integer'
        read *,i
        print*,'Here is i',i
        i = i + 1
        print *,'Here is the new i',i
        i = i + 1
        print *,'Here is the next i',i
        i = i + 1
        print *,'And here is the final i',i
        end


C Program #6
C This program shows simple if structure

C First declerations
        integer number

C Now input the value
        print *,'Enter an integer'
        read *,number

        if (number.eq.1) then
                print *,' you picked the number one'
        endif
        end

C Note that with the indent it is easier to read
C but this extra indent makes the program easier to read


C Program #7
C THis program shows more complicated if structure

C First declare the variable
        integer number

C Then input a number
        print *,'Enter an integer'
        read *, number

        if (number.eq.1) then
                print*,'That was the number one'
        elseif (number.gt.7) then
                print*,'Greater than 7'
        elseif (number.le.-4) then
                print *,'less than or equal to -4'
        else
                print *,'I don''t know what to do'
        endif
        end


C Program #7
C THis program shows more complicated if structure

C First declare the variable
        integer number

C Then input a number
        print *,'Enter an integer'
        read *, number

        if (number.eq.1) then
                print*,'That was the number one'
        elseif (number.gt.7) then
                print*,'Greater than 7'
        elseif (number.le.-4) then
                print *,'less than or equal to -4'
        else
                print *,'I don''t know what to do'
        endif
        end


C program #8
C This program demonstates a while loop

C declerations
        integer number, i
C something new, set a value to one of the numbers
        i = 1

C main
        print *,'Enter an integer, less than -100 to exit'
        read *,number

        while (number.gt.-100) do
                print*,'THis is in a while loop'
                print*,i
                i = i + 1
                print *,'enter an integer, less than -100 to exit'
                read *,number
        endwhile
        print *,'Now you are out of the while loop.'
        end

C program #9
C This program demonstrates a do loop (right out of lab 1)

C declerations
        integer a

C main
        do 10 a = 0,50,5
                print *,a
10      continue
        end



C program #10
C this program demonstrates a do loop with variables

C declerations
        integer loop, start, ending
        start = 5
        ending = 16

C main
        do 53, loop = start,(ending-1)
                print*, loop
53      continue
        end



C program #11
C this program contains an array

C declerations
        integer array(5)

C main
C first input numbers into the array
        do 67 i = 1,5
                print*,'Enter array element #',i
                read*,array(i)
67      continue

C now all the information is in the array, we can look at
C elements individually like this

        print *,'----------------------------------------'
        print *,'The 4th element is',array(4)
        print *,'The first element is',array(1)
        print *,'----------------------------------------'

C We can also print everything up in a loop

        do 34 i = 1,5
                print *,'Here is array element',i
                print *,array(i)
34      continue
        end