Search this site
Embedded Files
Homepage
  • Home
  • Courses
  • Physics - Theory
  • Physics - Experiment (Virtual-Lab)
  • Physics - Simulation
  • Computer Programming
  • Physics-SImulation (Undergrad)
  • Research
  • Current Research Direction (Simulation)
  • Job/Phd Opportunity
Homepage

Online Fortran programming tutorials :

  1. LEARN Fortran : Fortran Tutorial

  2. Getting started with Fortran

  3. Notes

Fortran download link (For desktop/laptop) : Link

Fortran for android device : Link to download and watch how to configure it.

Run Fortran programming in my webpage without installing it in your devices

Tips :


  1. Left : Below main.f90, write or pest your Fortran code

  2. Right : If the program needs external input, put it below the STDIN

Programming Code Examples:

Programming Code : Even-Odd finding

program even_odd


! define variables

implicit none

integer i1

character(4) num


!Input the interger to find whether it is even or odd

print *, 'Insert one integer, then press ENTER'

read *, i1


! main body for checking ever or odd

if (mod(i1,2).eq.0) then

num='even'

else

num='odd'

end if


! output

print *, 'the inserted number is ',num

end

Result after execution :

Input (Below STDIN) :

5


Click Run


Output (below STDOUT):

Insert one integer, then press ENTER

the inserted number is odd

Programming Code : Factorial n!

program factorial

implicit none


! define variables

integer :: nfact=1, n=1, nmax


!Input the interger for finding its factorial

print*, 'Insert one integer, then press ENTER'

read*, nmax


! compute factorials

do while (n<=nmax)

nfact=nfact*n

n=n+1

end do


! output

print *, nmax, "!=", nfact

end program factorial


Program execution :


Input (below STDIN) :

5


Click Run


Output (below STDOUT) :

Insert one integer, then press ENTER

5 != 120

Programming Code : Root finding

! a x^2 + b x + c = 0

program Root_Eq


! define variables

implicit none

real a, b, c, d, r1, r2, rr2,rc2


! input parameters of Equation

print *, "give a, b, c"

read *, a, b, c


! compute d

d=b*b-4*a*c


! writing the solutions

if (d>=0) then

r1=(-b+sqrt(d))/(2*a)

r2=(-b-sqrt(d))/(2*a)

print *, "Real solutions are :x1=", r1,"x2=", r2

else

rr2=-b/(2*a)

rc2=(sqrt(abs(d)))/(2*a)

print *, "Complex solutions are :x1", rr2,"+i",rc2 , ", x2=", rr2,"-i",rc2

end if

end program Root_Eq

Program execution :


Input (below STDIN) :

1 1 5


Click Run


Output (below STDOUT):

give a, b, c

Complex solutions are :x1 -0.500000000 +i 2.17944956 , x2= -0.500000000 -i 2.17944956


Programming Code : Use of Functions : Find the maximum value of a one dimensional array

! use of functions

! define variables

implicit none

real, dimension(10) :: i

real :: maximum

integer :: j


print *, "enter 10 integer numbers, separated by space ?"

read *, (i(j), j=1,10)


! display the maximum value

print *, 'maximum value is = ',maximum(i,10)

end


! function to find the maximum value

function maximum(iarray, nsize)

implicit none

real :: maximum

integer :: nsize, j

real, dimension(nsize) :: iarray

maximum=iarray(1)

do 10 j=2,10,1

if (maximum.lt.iarray(j)) then

maximum=iarray(j)

end if

10 continue

return

end

! note that the value of maximum will return when the function “maximum(iarray, nsize)” is called.

Program execution :

Input (Below STDIN) :

1 2 3 4 5 6 7 20.79 7 4 1 100.7


Click Run


Output (Below STDOUT) :

enter 10 integer numbers, separated by space ?

maximum value is = 20.7900009

Programming Code : Use of Subroutines : find the maximum and minimum values of a one dimensional array ?

! use of subroutines

implicit none

integer :: j

real (kind=8) :: max, min

real (kind=8), dimension(10) :: a ! or real(8) :: a(10)

print *, 'enter 10 integer numbers, separated by space ?'

read *, (a(j),j=1,10)


! display the maximum value

call maxmin(a,10,max,min)

print*, 'maximum value is = ',max

print*, 'minimum value is = ',min

end


! subroutine to find the maximum and minimum values

subroutine maxmin(iarray, nsize, max, min)

implicit none

real (kind=8), dimension(nsize) :: iarray

real (kind=8) :: max, min

integer ::j, nsize

max=iarray(1)

min=iarray(1)

do 10 j=2,10,1

if (max.lt.iarray(j)) then

max=iarray(j)

end if

if (min.gt.iarray(j)) then

min=iarray(j)

end if

10 continue

return

end

!Note : To bring SUBROUTINES into operation we write a CALL statement. real (kind=8) : implies higher working precision

Program execution :


Input (below STDIN) :

1 2 45.678 4 0 -3.1456 7 8 9 2 3


Click Run


Output (STDOUT):

enter 10 integer numbers, separated by space ?

maximum value is = 45.677999999999997

minimum value is = -3.1456000000000000


Programming Code : Calculating Euler number using exp(x) series evaluated at x=1

! Calculating Euler number using exp(x) series evaluated at x=1

implicit none

integer :: i, od

real (kind = 8) :: sum1, x, fact, exp

print*, "give the maximum order of expansion of Exp(x)"

read*, od

x=1

sum1=1.0

do i=1,od,1

sum1=sum1 + x**i/fact(i)

end do

print*, "Euler number=",sum1, "with error", sum1-exp(1.0)

end


! function to find the factorial

function fact(a)

implicit none

integer :: a, i

real (kind = 8) :: fact

fact=1

if (a.eq.0) then

fact=1

else

do i=1,a,1

fact=fact*i

end do

continue

end if

return

end

Program execution :


Input (below STDIN)

25

Click Run

Output(below STDOUT) :

give the maximum order of expansion of Exp(x)

Euler number= 2.7182818284590455 with error 8.2548401003634808E-008



Programming Code : Compile a frequency distribution and evaluate mean, standard deviation etc.

! Mean Variance Standard Deviation

implicit none

real, dimension(20) :: i

real(8) :: avg, var, var2, sd, median

integer :: j ,nmax=20

print *, "enter 20 data set, separated by space ?"

read *, (i(j), j=1, nmax)

avg=sum(i)/nmax

var=(sum(i*i))/nmax- avg**2

!var2=(sum(i*i) - (sum(i))**2/nmax)/(nmax-1)

sd=sqrt(var)

if (mod(nmax,2).eq.0) then

median=(i(nmax/2)+i(nmax/2+1))/2

else

median=i((nmax+1)/2)

endif

! display the resule

print *, "avg = ",avg, ", var=", var, ", sd=",sd, ", median=", median

end

Program execution :


Input (below STDIN)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20


Click Run


Output(below STDOUT) :

enter 20 data set, separated by space ?

avg = 10.500000000000000 , var= 33.250000000000000 , sd= 5.7662812973353983 , median= 10.500000000000000


Programming Code : Evaluate sum of finite series and the area under a curve.

! sin series

implicit none

!real, dimension(20) :: i

real(8) :: sinser,x, sum1=0

integer(8) :: i, j ,nmax,iod,nfact

print *, "number of terms of the series"

read *, nmax

print *, "print value of argument"

read *, x

do i=0,nmax,1

sum1=sum1+(-1)**i*x**(2*i+1)/nfact(2*i+1)

enddo

sinser=sum1

print *, "sin(",x,")=",sinser, "error=", sinser-sin(x)

end

! function to find the factorial

function nfact(ni)

implicit none

! define variables

integer(8) :: ni, n=1,nfact,prod=1

! compute factorials

do while (n<=ni)

prod=prod*n

n=n+1

enddo

nfact=prod

return

end

Program execution :


Input (below STDIN : needs to put two different inputs in two different lines)

5

1.5708

Click Run

Output(below STDOUT) :

number of terms of the series

print value of argument

sin( 1.5708000000000000 )= 0.99999994373259715 error= -5.6260656577933332E-008

Programming Code : Evaluate the area under a curve.

! area under a Sine-curve

implicit none

real(8) :: x, sum1=0, a, b, func, step

integer(8) :: i, nmax

print *, "Range of integration : a b"

read *, a, b

print *, "Number of slice of the range [a, b]"

read *, nmax

do i=0,nmax,1

x=a + (b-a)*i/nmax

step=(b-a)/nmax -a ! dx

sum1=sum1+step*func(x)

enddo

print *, "Area=",sum1

end

! function of the corresponding curve

function func(x)

implicit none

! define variables

real(8) :: func, x

func=sin(x)

return

end

Program execution :


Input (below STDIN)

0 3.141592653589793

1000


Click Run


Output(below STDOUT) :

Range of integration : a b

Number of slice of the range [a, b]

Area= 1.9999983550656637


Back

Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse