next up previous
Next: The Fortran Language Up: CS101 Lecture 4 Previous: Simple Unix Commands

A Simple Fortran Example

! A toy program to demo Fortran. 
!         By  siva@cse.iitb.ernet.in (July 28, 2000)
! It computes a series  1/1^2 + 1/2^2 + 1/3^2 + ... 1/m^2
! upto  m terms where m is input by the user.
! Start with a name for the prorgam
program series
 ! First declare the variables and their TYPES
   integer :: n,m
   real :: sum_series       ! sum is a keyword
 ! Initialize the answer
   sum_series = 0.0
 ! Ask user how many terms she wants to evaluate
   print *, "How many terms do you want? -> "
 ! Read the value given.
   read *, m
 ! we are assuming a co-operative user who will not type junk!
!  add the first m terms of a simple series
   do n = 1, m
      sum_series = sum_series + 1.0/real(n)**2
      print *, n,sum_series
   end do
end program series
Let's run it now.



G. Sivakumar 2000-08-02