Introductory Programming Examples

A Simple Welcome Program

     Program greet

     !every program has a name

     !execution of this program results in the display of

     !of the welcome message

 

     implicit none

     !compiler directives or declarations

     !this is legacy; ALWAYS include this line in your program

 

     print * , "Hi! Welcome to the session“

     !body with just one statement

     end program greet

Execution of the Program

•      To execute the program

–  First compile the program

            f90 –o greet greet.f90

–  This results in an executable greet

–  Run greet (invoke in the command line)

•      When greet is run, it prints

       Hi! Welcome to the session

A More interesting program

Program greet1

implicit none

character(len = 20):: name

!name - variable that stores character string of length less than or equal to 20

print *, "Hi! Welcome to the session“

print *, "PL. tell me your name“

read *, name

!waits for the user to give a character string

!this string is then stored in "name“

print *, "Hi,",name

print *, "How are you“

end greet1

Some Observations

•      Print and Read statements

•      Variables high level abstractions of memory

•      Variables store values input by you

•      Stored values can be read back and printed

•      Values are character strings

Behavior of greet1

•      Compile and execute the object file

•      It first prints

          Hi! Welcome to the session

          PL. tell me your name

•      It then waits for you to input the name

•      The input string(Hari),  is read into name

•      Finally it prints

            Hi!  Hari

            How are you?

Another variation

program greet2

!asks for first and surname

implicit none

character(len = 10):: first_name, sur_name

! two variables are declared

 

character(len = 20):: full_name

!another variable of length 20

print *, "Hi! Welcome to the session“

print *, "PL. tell me your first name“

Program Continued

!waits for the user to give a character string

read *, first_name

read *, sur_name

full_name = first_name//" "//sur_name

!assignment statement

!rhs is an expression which is evaluated

!resulting value is stored in lhs variable

!// is string concatenation operator

print *, "Hi,",full_name," How are you“

end greet2

 

A Problem

•      Examine the output of the above program, when the input is given as

             Dinesh Mongia

•       the program will outputs

             Dinesh      Mongia

•      There are 4 blank spaces after `Dinesh' (why?)

 

•      How do we remove the blanks?

 

•      Here is the program

A Solution

program greet3

implicit none

character(len = 10):: first_name,sur_name

character(len = 20):: full_name

print *, "Hi! Welcome to the session“

print *, "PL. tell me your first name“

read *, first_name

read *, sur_name

full_name = trim(first_name)//" "//trim(sur_name)

!trim is an intrinsic function that removes trailing blanks

print *, "Hi,",full_name," How are you“

end greet3

A simple arithmetic function

Program sum_and_multiply

implicit none

integer :: x,y,sum,prod

!declaration of integer variables

read *, x,y

sum = x + y

!value in x and y are added and stored in sum

prod = x * y

print *, "Sum of ", x,  " and ", y, " is = ",sum

print *, "Product of ", x,  " and ", y,  " is = ",prod

end program sum_and_multiply

Another Simple Program

     Program convert_to_seconds

    implicit none

    integer :: hour, minute, in_second, out_second

    read *, hour, minute, in_second

    out_second = in_second
out_second = out_second + (minute * 60)
out_second = out_second + (hour * 60 * 60)

print *, out_second
end program convert_to_seconds


 

Observation

•      Complex algebraic expressions allowed on the right hand side of assignment statement

•      Same variable can appear both in the right hand and left hand sides

•      Rhs occurrence refers to the current content

•      Lhs occurrence refers to the location

Interest Calculation

Program interest

implicit none

real:: principle, year, rate, inter, total

!declaration of real variable

 

read *, principle, year, rate

inter = (principle*year*rate)/100

total = principle + inter

print *, inter, total

end program interest

 

How to compute compound interest?

Distance Calculation

program pt_distance !computes distance between two pts.

implicit none

real :: a,b,c,d,distance  !Two points are (a,b) and (c,d)

print * , "type values of a, b, c, d“

read * , a, b, c, d

distance = sqrt((a - c) ** 2+(b - d) ** 2)

  !sqrt intrinsic function on real values; argument an expn.

print * , "distance between points (", a, &

 ",", b, ") and (", c, ",", d, ") is", distance

   !note & at the end of first line – denotes Continuation

   !length of a line can not exceed 132 characters

end program point_distance