Here you are some samples of code written in Fortran
print *,"Hello World!"
end program hello
remember that Fortran is case unsensitive so the following code is the same as above:
PROGRAM HEllo
pRint *,"Hello World!"
End proGram hello
C AREA OF A TRIANGLE – HERON'S FORMULA
C INPUT – CARD READER UNIT 5, INTEGER INPUT, NO BLANK CARD FOR END OF DATA
C OUTPUT – LINE PRINTER UNIT 6, REAL OUTPUT
C INPUT ERROR DISPAYS ERROR MESSAGE ON OUTPUT
501 FORMAT(3I5)
601 FORMAT(" A= ",I5," B= ",I5," C= ",I5," AREA= ",F10.2,"SQUARE UNITS")
602 FORMAT("NORMAL END")
603 FORMAT("INPUT ERROR OR ZERO VALUE ERROR")
INTEGER A,B,C
10 READ(5,501,END=50,ERR=90) A,B,C
IF(A=0 .OR. B=0 .OR. C=0) GO TO 90
S = (A + B + C) / 2.0
AREA = SQRT( S * (S – A) * (S – B) * (S – C))
WRITE(6,601) A,B,C,AREA
GO TO 10
50 WRITE(6,602)
STOP
90 WRITE(6,603)
STOP
END
Greatest Common Divisor
* euclid.f (FORTRAN 77)
* Find greatest common divisor using the Euclidean algorithm
PROGRAM EUCLID
PRINT *, 'A?'
READ *, NA
IF (NA.LE.0) THEN
PRINT *, 'A must be a positive integer.'
STOP
END IF
PRINT *, 'B?'
READ *, NB
IF (NB.LE.0) THEN
PRINT *, 'B must be a positive integer.'
STOP
END IF
PRINT *, 'The GCD of', NA, ' and', NB, ' is', NGCD(NA, NB), '.'
STOP
END
FUNCTION NGCD(NA, NB)
IA = NA
IB = NB
1 IF (IB.NE.0) THEN
ITEMP = IA
IA = IB
IB = MOD(ITEMP, IB)
GOTO 1
END IF
NGCD = IA
RETURN
END
program add
c
c John Mahaffy, Penn State University, CmpSc 201 Example
c 1/26/96
c
c
c This is a simple program to read 2 numbers and print the sum
c
implicit none
real a,b,s
c
c a – one of two numbers to be added
c b – the other number in the sum
c s – the sum of a and b
c
c Get the numbers from the program user
c First ask for the numbers
c
print *, ' This program adds 2 real numbers'
print *, ' Type them in now separated by a comma or space'
c
c Now read the numbers that are typed by the user
c this Fortran read will wait unil the numbers are typed
c
read *, a,b
c
c Now calculate the sum
c
s = a + b
c
c Print out the results with a description
c
print *, 'The sum of ', a,' and ' , b
print *, ' is ' , s
stop
end
Formatting parameters
program format
c
c Examples of various uses of the Format Statement, and a small
c warning about precision of constants.
c
c John Mahaffy 2/26/96
c
integer n
parameter (n=5)
real r,cv,t(n),p(n),rho(n),u(n),tc(n),tf(n)
double precision rd,cvd,td,pd,rhod
double precision rd1
c</font></a>
parameter (r=287.0478,cv=1004.832–r)
c
c Watch the output to see what happens when I casually move
c single precision numbers into double precision parameters
c (or constants). The computer is pretty causual about what
c it fills into the extra digits of precision.
c
parameter (rd1=r)
c
c Here I carefully define constants as DOUBLE PRECISION
c
parameter (rd=287.0478d0,cvd=1.004832d3–r)
c
c In Fortran90 I could have used a different notation in conjuction
c with KIND. Comment out old code and uncomment the following to try.
c
c integer, parameter :: r8=selected_real_kind(15,30)
c real (r8) rd,cvd,td,pd,rhod,ud
c parameter (rd=287.0478_r8, cvd=1.004832e3_r8)
c
data p,t /n*1.e5,n*300./
data pd,td/1.d5,3.d2/
open(11,file='format.out')
print *, ' Casual Definition of Double Precision Gas Constant ='
& ,rd1
print *, ' Careful Definition of Double Precision Gas Constant ='
& ,rd
c
print *
c
print 2222
2222 format ('Look what I''ve got to do to include a single quote')
c
print *
c
rho(1)= p(1)/(r*t(1))
rhod=pd/(rd*td)
c
c Start by writing a header
c
write(*,1000)
1000 format(14x,' Pressure',13x,'Temperature',17x,'Density')
write(*,2000) p(1),t(1),rho(1)
write(*,2001) pd,td,rhod
write(*,2002) pd,td,rhod
c
c Note how numbers in the following are right justified in the output
c
2000 format(1p,3(1x,e23.7))
2001 format(1p,3(1x,d23.15))
c
c You can get away with "e" edit descriptor on DOUBLE PRECISION
c
2002 format(1p,3(1x,e23.15))
tc(1)=t(1)-273.16
tf(1)=32.+1.8*tc(1)
u(1)=cv*t(1)
rho(1)=p(1)/(r*t(1))
do 100 i=2,n
t(i)=t(i–1)+100.
tc(i)=t(i)-273.16
tf=32.+1.8*tc(i)
p(i)=p(i–1)+1.e4
u(i)=cv*t(i)
100 rho(i)=p(i)/(r*t(i))
write(11,2020) 2005
write(11,2004)
2004 format(//,63x,'Internal',/,
& 1x,'Cell',3(5x,'Temp '), 7x,'P',8x,'Density',6x,'Energy',
& /,2x,'Num',6x,'(K)',7x,'(C)',7x,'(F)',6x,'(Pa)',6x,
& '(kg/m**3)',5x,'(J/kg)' )
write(11,2005) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2005 format(1x,i4,0p,3f10.1,1p,3e12.3)
c
c Watch What gets reused in the 2006 FORMAT
c
write(11,2020) 2006
write(11,2004)
write(11,2006) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2006 format(71('-'),/,(1x,i4,0p,3f10.1,1p,3e12.3))
c
c Watch What gets reused in the 2007 FORMAT, containing an extra
c bounding parentheses, and a repeat factor on the main interior
c block of the FORMAT
c
write(11,2020) 2007
write(11,2004)
write(11,2007) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2007 format((71('-'),/,1(1x,i4,0p,3f10.1,1p,3e12.3)))
c
c Next I get fancy with Repeat Operators and some vertical lines
c
write(11,2020) 2008
write(11,2004)
write(11,2008) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2008 format((1x,i4,0p,3(2x,'|',2x,f5.1),1p, 3(1x,'|',1x,e9.3)))
c
write(11,2020) 2009
write(11,2004)
c
c Look how the seemingly harmless drop of one pair of parentheses
c can totally wreck the pattern of reuse in the FORMAT
c
write(11,2009) (i,t(i),tc(i),tf(i),p(i),rho(i),u(i),i=1,n)
2009 format(1x,i4,0p,3(2x,'|',2x,f5.1),1p, 3(1x,'|',1x,e9.3))
c
c Try replacing the above failed format with one that produces a
c table containing both vertical and horizontal bounding lines.
c
2020 format(//,' Format Number ',i4)
stop
end
Most of sample codes are taken from http://www.personal.psu.edu/jhm/f90/progref.html
Gg1