You are on page 1of 11

CF Lab Assignment 1

Task 1:
Attempt the program given in literature above. Also see whether the results are as
expected?

1-Programs without declaration statement


Codes for the program:
! This program is to go through basic functions of fortran 77
print*,'' ! intentionally left
print*,'--------LAB 1 TASK 1 (without declaration)-------------'
print*,''
print*,'

Exercise 1'

print*,'I am a student of masters'


print*,''
print*,''
print*,'

Exercise 2'

print*,'2','+','2','=',4
print*,''
print*,''
print*,'

Exercise 3'

print*,'2+2=',4
print*,''
print*,''
print*,'

Exercise 4'

k=40 ! 40 is an integer so k is not declared in variable declaration


print*,'This will print',k
print*,''
print*,''

end

AHMED SIDDIQUE

MS-25710

Results:

2-Programs with declaration statement:


Codes for the program:
! This is an introductory program & includes declaring the variables and user input
real a,b,c,length1,length2,angle
! where a,b,c are used for average calculation problem
! lenght1 and length2 are used in READ command demonstration
! angle is used to in demonstration of trigonometric functions
print*,'-------LAB 1 TASK 1 (including declaration statement)-----------'
print*,''
print*,'

Exercise 1 (compute average)'

a=10
b=17
c=20

AHMED SIDDIQUE

MS-25710

print*,''
print*,''
print*,'average of ',a,',',b,'&',c,'is',(a+b+c)/3
print*,''
print*,'

Exercise 2 (READ command demonstration)'

print*,''
print*,''
print*,'Enter lengths:'
read*,length1,length2
print*,''
print*,'The lengths are',length1,'&',length2
print*,''
print*,'

Exercise 3(average with user based input)'

print*,''
print*,''
print*,'Enter the value of a:'
read*,a
print*,'Enter the value of b:'
read*,b
print*,'Enter the value of c:'
read*,c
print*,''
print*,'average of ',a,',',b,'&',c,'is',(a+b+c)/3
print*,''
print*,'

Exercise 4 (Trigonometric function)'

print*,''
print*,''
print*,'Enter the value of angle in degrees:'
read*,angle
print*,'sin (',angle,') =',sin(3.14159*angle/180)

AHMED SIDDIQUE

MS-25710

end

Results:

Task 2:
In the example given in section 4.3, declare another variable named average, save
the average
value in this variable and print this variables value instead of the expression
directly.

Codes for program:


program user input average
real a,b,c,avg
AHMED SIDDIQUE

MS-25710

a, b & c are the numbers whose average is to be calculated


print*,''
print*,''
print*,'--------LAB 1 TASK 2---------'
print*,'' ! intentional spaces for better look
print*,''
! Input
print*,'Enter the value of a:'
read*,a
print*,''
print*,''
print*,'Enter the value of b:'
read*,b
print*,''
print*,''
print*,'Enter the value of c:'
read*,c
print*,''
print*,''
avg=(a+b+c)/3
print*,'average of ',a,', ',b, '& ',c, 'is ',avg
end program user input average

Results:

AHMED SIDDIQUE

MS-25710

Task 3:
Checking the order in which mathematical operations are performed in fortran.

Codes for program:


print*,''
print*,'----------LAB 1 TASK 3-----------'
print*,''
print*,''
! check for effect of different arithmetic operations
print*,5.+2.*3.,',',(5.+2.)*3.,',',2.*3.**3.,',',(2.*3.)**3.,','
1,(2.)**(3.)**2. ! statement is continued from previous line
! the result of this program indicates the order in which mathematical
functions are solved in fortran
! for given case order is parenthesis>exp>multiplication>addition

AHMED SIDDIQUE

MS-25710

End

Results:

Task 4:
Take the angle from user, compute the trigonometric quantities: sine, cosine,
tangent, sin-1, cos-1, tan-1and print the results in the following format:
sin(angle value) = Computed value
asin(angle value) = Computed value
cos(angle value) = Computed value
acos(angle value) =
Computed value
tan(angle value) = Computed value
atan(angle
value) = Computed value

Codes for program:


program Trigonometric functions
real z,x,y1,y2,y3,y4,y5,y6
! variable z for angle input and x to denote radian y is output
print*,'' ! space intentionally left
print*,''
print*,'-----------LAB 1 TASK 4-----------'
print*,''
print*,''
print*,'Enter the value of angle in degrees:'
print*,''

AHMED SIDDIQUE

MS-25710

read*,z
print*,''
print*,''
x=z*3.14159/180
y1=sin(x)
y2=(180/3.14159)*(asin(y1))
y3=cos(x)
y4=(180/3.14159)*(acos(y3))
y5=tan(x)
y6=(180/3.14159)*(atan(y5))
print*,'sin(',z,') = ',y1,'

asin(',y1,') = ',y2

print*,''
print*,''
print*,'cos(',z,') = ',y3,'

acos(',y3,') = ',y4

print*,''
print*,''
print*,'tan(',z,') = ',y5,'

atan(',y5,') = ',y6

end program Trigonometric functions

Results:

Task 5:

AHMED SIDDIQUE

MS-25710

Find the hypotenuse of a triangle by taking base and perpendicular from the user.
Print the computed value.

Codes for program:


program calculation of hypotenuse of triangle
real perp,base,hyp
print*,'' ! intentional space
print*,'-----------LAB 1 Task 5----------'
print*,''
print*,''
c

INPUT
print*,'Enter the value of perpendicular in cm:'
print*,''
read*,perp
print*,''
print*,''
print*,'Enter the value of base in cm:'
print*,''
read*,base

Execution
hyp=sqrt(((perp)**2)+(base**2))

Results
print*,''
print*,''
print*,'hypotenuse of triangle with ',perp,'cm perpendicular and'
1,base,'cm base is',hyp
print*,''
end program calculation of hypotenuse of triangle

Results:

AHMED SIDDIQUE

MS-25710

Task 6:
Take input from user the initial number of atoms, half-life and the time that an atom
has spent to calculate the atoms remaining?

Codes for program:


program Calculation of remaining radioactive atoms
real n,no,t,thalf,lamda
print*,''
print*,'--------LAB 1 TASK 6--------'
print*,''
print*,''
c

INPUT
print*,'Enter the following values:'
print*,''
print*,'initial number of atoms:'
print*,''
read*,no
print*,''
print*,''

AHMED SIDDIQUE

MS-25710

print*,'time of observation (seconds)'


print*,''
read*,t
print*,''
print*,''
print*,'half life of element (seconds)'
print*,''
read*,thalf
print*,''
print*,''
c

EXECUTION
lamda=0.693/thalf
n=no*(exp(-lamda*t))

RESULTS
print*,'NO. of atoms after ',t,' seconds = ',n
end program Calculation of remaining radioactive atoms

Results:

AHMED SIDDIQUE

MS-25710

You might also like