Example of Fortran code : kinetic energy

 

program kinetic_energy
implicit none
real K, m, v ! declare the types of these variables

open(unit=1, file=’result’, status=’replace’) ! open a file to store results

!——————————————————————————!
! Request for input from user !
!——————————————————————————!
print*, “Give the mass of the object (in unit of kilogram)”
read*, m

print*, “Give the velocity of the object (in unit of meter per second)”
read*, v

!——————————————————————————!
! Calculation of kinetic energy !
!——————————————————————————!
K = m*(v**2.0)/2.0
! K = m*(v*v)/2.0 !This form is also possible

!——————————————————————————!
! Print results on the terminal !
!——————————————————————————!
print*
print 100, “The kinetic energy of the object is”, K, “Joules”
100 format(a,1x,f8.3,1x,a)

!——————————————————————————!
! Print results into the file !
!——————————————————————————!
write(1,'(a,1x,f8.3,1x,a)’) “The kinetic energy of the object is”, K, “Joules”

!——————————————————————————!
! Close result file !
!——————————————————————————!
close(1)

end program