You are on page 1of 28

PAKISTAN INSTITUTE OF ENGINEERING AND

APPLIED SCIENCES, ISLAMABAD


Computing Fundamentals-I (Lab-I)

“CF-I Lab Assignment 1”.

Subject:

Computing Fundamentals-I (Lab)


Submitted To:
Dr. Rizwan Ahmad

Submitted By:
Ihtisham Ul Haq

Registration Number:
MS-22-AM-504821

Department Mechanical Engineering


Pakistan Institute of Engineering and Applied Sciences, Islamabad

IHTISHAM UL HAQ MS-22-AM-504821 Page|1


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Task 1.
a) Repeat all the examples of CF-1 Lab Assignment.
Example_1.
Programming Code:

program test01
print* , 'Hello world' ! A message
print* , '2+2=' ,2+2 !Addition operation
print* , '5/3=' ,5/3 !Division operation
print* , '5.0/3=' ,5.0/3
print* , 'sin (30)=' ,sin(30.0) !Trigonometric function sine
print* , "sin (30.0/180*3.14159268)=" ,sin (30.0/180*3.14159268)
end program test01

Screen shot of the Programming code:

Output:

Hello world
2+2= 4
5/3= 1
5.0/3= 1.66666663
sin (30)= -0.988031626
sin (30.0/180*3.14159268) = 0.500000000

IHTISHAM UL HAQ MS-22-AM-504821 Page|2


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Screen shot of output:

=========================End Example_1===========================

Example_2.
Programming Code:

program Arith
a = 2.500 !input
b = 7.030
!=========================== Calculations
c = a+b !Addition
d = a-b !Subtraction
e = a*b !Multiplication
f = a/b !Division
g = a**b !Exponentiation
!===========================Output
Print* , "Value_of_a =" ,a
Print* , "Value_of_b =" ,b
Print* , "Value_of_c =" ,c
Print* , "Value_of_d =" ,d
Print* , "Value_of_e =" ,e
Print* , "Value_of_f =" ,f
Print* , "Value_of_g =" ,g
end program Arith

Output:
Value_of_a = 2.50000000
Value_of_b = 7.03000021
Value_of_c = 9.53000069
Value_of_d = -4.53000021
Value_of_e = 17.5750008
Value_of_f = 0.355618775
Value_of_g = 627.362183

IHTISHAM UL HAQ MS-22-AM-504821 Page|3


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Screen shot of the code:

Screen shot of output:

=========================End Example_2===========================

IHTISHAM UL HAQ MS-22-AM-504821 Page|4


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Example_3.
Programming Code:

program HYP_FUN
print*, "HYPERBOLIC FUNCTIONS"
print*, "sinh(x) function sinh(0.0) =" , SINH(0.0)
print*, "cosh(x) function cosh(0.0) =" , COSH(0.0)
print*, "tanh(x) function tanh(0.0) =" , TANH(0.0)
end program HYP_FUN

Screen shot of the Programming code:

Output:
HYPERBOLIC FUNCTIONS
sinh(x) function sinh(0.0) = 0.00000000E+00
cosh(x) function cosh(0.0) = 1.00000000
tanh(x) function tanh(0.0) = 0.00000000E+00

Screen shot of output:

=========================End Example_3===========================

IHTISHAM UL HAQ MS-22-AM-504821 Page|5


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Example_4.
Programming Code:

program OTHER_FUN
print*, "OTHER MATHEMATICAL FUNCTIONS"
print*, "sqrt(x) function SQRT(9.0) =" , SQRT(9.0)
print*, "abs(x) function ABS(-5.5) =" , ABS(-5.5)
print*, "exp(x) function EXP(0.0) =" , EXP(0.0)
print*, "alog(x) function ALOG(2.0) =" , ALOG(2.0)
print*, "alog10(x) function ALOG10(10.0) =" , ALOG10(10.0)
end program OTHER_FUN

Screen shot of the Programming code:

Output:
OTHER MATHEMATICAL FUNCTIONS
sqrt(x) function SQRT(9.0) = 3.00000000
abs(x) function ABS(-5.5) = 5.50000000
exp(x) function EXP(0.0) = 1.00000000
alog(x) function ALOG(2.0) = 0.693147182
alog10(x) function ALOG10(10.0) = 1.00000000

Screen shot of output:

=========================End Example_4===========================

IHTISHAM UL HAQ MS-22-AM-504821 Page|6


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Task 1.
b) Use the library functions in the table shown above in simple programs to get hold on
how they are used. You may refer to the document (file name: ‘ANSI FORTRAN77-
1978.pdf’) in the Fortran books folder for details. Test against at least five examples
and compare your results with the results of your calculator.

Library function No:01. Programming Code:

program Library_Fun_01

print*, "absolute value of given number"

print*, "ABS(-19.8) =" , ABS(-19.8) !compute the absolute/modulus of -19.8


print*, "ABS(16) =" , ABS(16) !compute the absolute/modulus of 16
print*, "ABS(4.6) =" , ABS(4.6) !compute the absolute/modulus of 4.6
print*, "ABS(-7) =" , ABS(-7) !compute the absolute/modulus of -7
print*, "ABS(-111) =" , ABS(-111) !compute the absolute/modulus of -111

end program Library_Fun_01

Screen shot of the Programming code:

IHTISHAM UL HAQ MS-22-AM-504821 Page|7


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Output:
absolute value of given number
ABS(-19.8) = 19.7999992
ABS(16) = 16
ABS(4.6) = 4.59999990
ABS(-7) = 7
ABS(-111) = 111

Screen shot of output:

Comparing with calculator:


Calculator Value:
|19.8| = 19.8
|16| = 16
|4.6| = 4.6
|-7| = 7
|-111| = 111
Comment: there is now error in integer and o very small error of 0.00000001% in real
number
======================End Library function No:01=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|8


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:02. Programming Code:


PROGRAM Library_Fun_02
print* , "Conversion of given numbers into double precision"
print* , "DBLE(5)=", DBLE(5) !Converting x into its double precision
print* , "DBLE(13.05)=", DBLE(13.05)
print* , "DBLE(4.1)=", DBLE(4.1)
print* , "DBLE(35.0)=", DBLE(35.0)
print* , "DBLE(9)=", DBLE(9)
print* , "=========================================="
END PROGRAM Library_Fun_02

Screen shot of the Programming code:

Output:
DBLE(5)= 5.
DBLE(13.05)= 13.0500002
DBLE(4.1)= 4.0999999
DBLE(35.0)= 35.
DBLE(9)= 9.

Screen shot of output:

Comparing with calculator:


The result shows no error in converting the integer bust very small errors of converting real to
double precision.
======================End Library function No:02=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|9


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:03. Programming Code:


PROGRAM Library_Fun_03
print* , "Double precision product of given numbers"
print* , "DPROD(2.0,6.05)=" , DPROD(2.0,6.05 )
print* , "DPROD(4.2,8.0)=" , DPROD(4.2,8.0 )
print* , "DPROD(9.1,19.0)=" , DPROD(9.1,19.0 )
print* , "DPROD(12.0,20.0)=" , DPROD(12.0,20.0 )
print* , "DPROD(13.5,14.0)=" , DPROD(13.5,14.0 )
END PROGRAM Library_Fun_03

Screen shot of the Programming code:

Output:
DPROD(2.0,6.05)= 12.1000004
DPROD(4.2,8.0)= 33.5999985
DPROD(9.1,19.0)= 172.900007
DPROD(12.0,20.0)= 240.
DPROD(13.5,14.0)= 189.

Screen shot of output:

Comparing with calculator:


Rounding off the result of Fortran become the same to the calculator value
======================End Library function No:03=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|10


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:04. Programming Code:


PROGRAM Library_Fun_04
print* , "Cosine values for numbers "
print*, "cos(1.0) =" , COS(1.0) !Computing cos(x)
print*, "cos(2.0) =" , COS(2.0)
print*, "cos(8.0) =" , COS(8.0)
print*, "cos(15.0) =" , COS(15.0)
print*, "cos(33.0) =" , COS(33.0)
END PROGRAM Library_Fun_04

Screen shot of the Programming code:

Output:
Cosine values for numbers
cos(1.0) = 0.540302277
cos(2.0) = -0.416146845
cos(8.0) = -0.145500034
cos(15.0) = -0.759687901
cos(33.0) = -0.0132767474

Screen shot of output:

Comparing with calculator:


Calculator Value: cos(1.0) = 0.5403023, cos(2.0) = -0.416146836, cos(8.0) = -
0.145500033, cos(15.0) = -0.759687912, cos(33.0) = -0.0132767477
The value is same up to 8 digits
======================End Library function No:04=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|11


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:05. Programming Code:


PROGRAM Library_Fun_05
print*, "LOG(1)=" , LOG(1.0)
print*, "LOG(2)=" , LOG(2.0)
print*, "LOG(3)=" , LOG(3.0)
print*, "LOG(4)=" , LOG(4.0)
print*, "LOG(5)=" , LOG(5.0)
END PROGRAM Library_Fun_05

Screen shot of the Programming code:

Output:
LOG(1)= 0.
LOG(2)= 0.693147182
LOG(3)= 1.09861231
LOG(4)= 1.38629436
LOG(5)= 1.60943794

Screen shot of output:

Comparing with calculator:


Calculator Value:
LOG(1)= 0. LOG(2)= 0.693147181, LOG(3)= 1.098612289, LOG(4)= 1.386294361
LOG(5)= 1.609437912
Comment: The value is same up to 8 digits
======================End Library function No:05=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|12


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:06. Programming Code:

PROGRAM Library_Fun_06
print*, "INT(8.4)=" , INT(8.4)
print*, "INT(17.3)=" , INT(17.3)
print*, "INT(25.5)=" , INT(25.5)
print*, "INT(33.5)=" , INT(33.5)
print*, "INT(49.34)=", INT(49.34)
END PROGRAM Library_Fun_06

Screen shot of the Programming code:

Output:

INT(8.4)= 8
INT(17.3)= 17
INT(25.5)= 25
INT(33.5)= 33
INT(49.34)= 49

Screen shot of output:

Comparing with calculator:


Calculator Value: The value is same after rounding off
======================End Library function No:06=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|13


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:07. Programming Code:


PROGRAM Library_Fun_07
print* , "Exponetial values of the given numbers"
print*, "EXP(0.0)=" , EXP(0.0) !Exponentiating x
print*, "EXP(1.0)=" , EXP(1.0)
print*, "EXP(5.0)=" , EXP(5.0)
print*, "EXP(9.0)=" , EXP(9.0)
print*, "EXP(27.0)=" , EXP(27.0)
END PROGRAM Library_Fun_07

Screen shot of the Programming code:

Output:

Exponetial values of the given numbers


EXP(0.0)= 1.
EXP(1.0)= 2.71828175
EXP(5.0)= 148.413162
EXP(9.0)= 8103.08398
EXP(27.0)= 5.32048249E+011

Screen shot of output:

Comparing with calculator:


Calculator Value:
EXP(0.0)= 1., EXP(1.0)= 2.718281828, EXP(5.0)= 148.413591. EXP(9.0)= 8103.083928
EXP(27.0)= 5.320482406E+011
Comment: The value is same up to 8 digits
======================End Library function No:07=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|14


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:08. Programming Code:


PROGRAM Library_Fun_08
print*, "MAX(7,7,45,88,99,76,56)=" , MAX(7,7,45,88,99,76,56) !MAximum of
x1, x2,... xn
print*, "MAX(6,9,34,88,96,34,89)=" , MAX(6,9,34,88,96,34,89)
print*, "MAX(12,76,9,5,4,85,99)=" , MAX(12,76,9,5,4,85,99)
print*, "MAX(55,76,45,87,34,87,79)=" , MAX(55,76,45,87,34,87,79)
print*, "MAX(99,88,67,67,55,74,23)=" , MAX(99,88,67,67,55,74,23)
END PROGRAM Library_Fun_08

Screen shot of the Programming code:

Output:

MAX(7,7,45,88,99,76,56)= 99
MAX(6,9,34,88,96,34,89)= 96
MAX(12,76,9,5,4,85,99)= 99
MAX(55,76,45,87,34,87,79)= 87
MAX(99,88,67,67,55,74,23)= 99

Screen shot of output:

Comparing with calculator:


Calculator Value: No error the values are same as to calculator
======================End Library function No:08=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|15


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:09. Programming Code:


PROGRAM Library_Fun_09
print* , "Square roots of given numbers"
print*, "SQRT(3.0)=" , SQRT(2.0) !Computing the square root of x
print*, "SQRT(4.0)=" , SQRT(4.0)
print*, "SQRT(46.0)=" , SQRT(46.0)
print*, "SQRT(25.0)=" , SQRT(25.0)
print*, "SQRT(144.0)=" , SQRT(144.0)
END PROGRAM Library_Fun_09

Screen shot of the Programming code:

Output:
Square roots of given numbers
SQRT(3.0)= 1.41421354
SQRT(4.0)= 2.
SQRT(46.0)= 6.78233004
SQRT(25.0)= 5.
SQRT(144.0)= 12.

Screen shot of output:

Comparing with calculator:


Calculator Value:
SQRT(3.0)= 1.417320508, SQRT(4.0)= 2. SQRT(46.0)= 6.7823299, SQRT(25.0)= 5.
SQRT(144.0)= 12.
Comment: The value is same for squared integer but has error of after 5 digits in non-squared
digits
======================End Library function No:9=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|16


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:10. Programming Code:


PROGRAM Library_Fun_10
print*, "MOD(90,20) =" , MOD(90,20) ! Computing MOD of (x,y)
print*, "MOD(220,30) =" , MOD(220,30)
print*, "MOD(10,10) =" , MOD(10,10)
print*, "MOD(22,35) =" , MOD(22,35)
print*, "MOD(325,345) =" , MOD(325,345)
END PROGRAM Library_Fun_1

Screen shot of the Programming code:

Output:
MOD(90,20) = 10
MOD(220,30) = 10
MOD(10,10) = 0
MOD(22,35) = 22
MOD(325,345) = 325

Screen shot of output:

Comparing with calculator:


Calculator Value:
No error:
======================End Library function No:10=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|17


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:11. Programming Code:


PROGRAM Library_Fun_11
print* , "NINT(7.8)=" , NINT(7.8) !rounded to nearest integer
print* , "NINT(9.9)=" , NINT(9.9)
print* , "NINT(6.2)=" , NINT(6.2)
print* , "NINT(4.3)=" , NINT(4.3)
print* , "NINT(5.5)=" , NINT(5.5)
END PROGRAM Library_Fun_11

Screen shot of the Programming code:

Output:
NINT(7.8)= 8
NINT(9.9)= 10
NINT(6.2)= 6
NINT(4.3)= 4
NINT(5.5)= 6

Screen shot of output:

Comparing with calculator:


Calculator Value:
The value is same after rounding off
======================End Library function No:11=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|18


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Library function No:12. Programming Code:


PROGRAM Library_Fun_12
print*, "REAL(10.4) =" , REAL(10.4) ! Real of x
print*, "REAL(20) =" , REAL(20)
print*, "REAL(34) =" , REAL(34)
print*, "REAL(40.05) =" , REAL(40.05)
print*, "REAL(50.60) =" , REAL(50.60)
END PROGRAM Library_Fun_12

Screen shot of the Programming code:

Output:
REAL(10.4) = 10.3999996
REAL(20) = 20.
REAL(34) = 34.
REAL(40.05) = 40.0499992
REAL(50.60) = 50.5999985

Screen shot of output:

Comparing with calculator:


Calculator Value:
The values are same after rounding off:
======================End Library function No:12=====================

IHTISHAM UL HAQ MS-22-AM-504821 Page|19


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Task 1.
c) Verify trigonometric and hyperbolic identities.
Programming Code:
program trigonometric_Fun

Print* , "trigonometric function"

print*, "sin(x) function sin(0.0) =" , SIN(0.0)


print*, "cos(x) function cos(0.0) =" , COS(0.0)
print*, "tan(x) function tan(0.0) =" , TAN(0.0)

print*, "hyperbolic function"

print*, "sinh(x) function sinh(0.0) =" , SINH(0.0)


print*, "cosh(x) function cosh(0.0) =" , COSH(0.0)
print*, "tanh(x) function tanh(0.0) =" , TANH(0.0)

end program trigonometric_Fun

Screen shot of the Programming code:

IHTISHAM UL HAQ MS-22-AM-504821 Page|20


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Output:
trigonometric function
sin(x) function sin(0.0) = 0.00000000
cos(x) function cos(0.0) = 1.00000000
tan(x) function tan(0.0) = 0.00000000
hyperbolic function
sinh(x) function sinh(0.0) = 0.00000000
cosh(x) function cosh(0.0) = 1.00000000
tanh(x) function tanh(0.0) = 0.00000000

Screen shot of output:

=========================End Lab_Task_01===========================

IHTISHAM UL HAQ MS-22-AM-504821 Page|21


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Task 2.
Compute the results (Write Fortran Statements to evaluate the following expressions
and compare your calculations with calculators)

Programming Code:
program Lab_1_task_02

! Given mathematical expressions


Y = 25.0**(1.0/2.0)*3.0**(1.0/3.0)
Z = 5.0*((11.0-5.0)**2)/(4.0)+9.0
F= 2*4*(5.0/2.0**3)
G = 100*(1+250.0/100.0)**3
X = (2.0*3.0)/(6.0*6.0+5.0*44.4)**(1.0/4.0)

print* , "Results for the given mathematical expressions"

print*, "1. Y = (25**(1/2)+3**(1/3)) =" , Y


print* , "2. Z = 5.0*(11.0-5.0**2)/4.0+9.0 =" , Z
print* , "3. F= 2*4*(5.0/2.0)**3 =" , F
print* , "4. G = 100*(1+250.0/100.0)**3 =" , G
print* , "5. X = (2.0*3.0)/(6.0*6.0+5.0*44.4)**(1.0/4.0) =" ,X

end program Lab_1_task_02

IHTISHAM UL HAQ MS-22-AM-504821 Page|22


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Screen shot of the Programming code:

Output:
Results for the given mathematical expressions
1. Y = (25**(1/2)+3**(1/3)) = 7.21124744
2. Z = 5.0*(11.0-5.0**2)/4.0+9.0 = 54.0000000
3. F= 2*4*(5.0/2.0)**3 = 5.00000000
4. G = 100*(1+250.0/100.0)**3 = 4287.50000
5. X = (2.0*3.0)/(6.0*6.0+5.0*44.4)**(1.0/4.0) = 1.49708462

Screen shot of output:

=========================End Lab_Task_02===========================

IHTISHAM UL HAQ MS-22-AM-504821 Page|23


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Task 3.
Write FORTRAN programs: (Define a, b, c and x in your programs and compute y in
these cases)

Programming Code:
program Lab_1_task_03

real y,a,b,c,x !decleation for Inputs


print*,'Write the values of a,b,c and x respectively'

read*, a,b,c,x !input variables


y=a*cos(x)+b*(cos(x))**2+(c*cos(x))**3
print*,'For a. Y = a*cos(x)+b*(cos(x))**2+(c*cos(x))**3 Y=',y

y=(a*x+b)/(a*x-b)
print*,'For b. Y = (a*x+b)/(a*x-b) Y=',y

y=2.5*log10(a)+cos(32*3.14/180.0)+abs(a**2.0-b**2.0)+ sqrt(2.0*a*b)
print*,'For c. Y = 2.5*log(a)+cos(32*3.14/180)+abs(a**2-b**2)+ sqrt(2*a*b) Y=',y

y=a*exp(-b*c)
print*,'For d. Y = a*exp(-a*b) Y=' ,y

end program Lab_1_task_03

Output:

Write the values of a,b,c and x respectively


2684
For a. Y = a*cos(x)+b*(cos(x))**2+(c*cos(x))**3 Y= -141.729630
For b. Y = (a*x+b)/(a*x-b) Y= 7.00000000
For c. Y = 2.5*log(a)+cos(32*3.14/180)+abs(a**2-b**2)+ sqrt(2*a*b) Y= 38.4997520
For d. Y = a*exp(-a*b) Y= 2.85032808E-21

IHTISHAM UL HAQ MS-22-AM-504821 Page|24


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Screen shot of the Programming code:

Screen shot of output:

=========================End Lab_Task_03===========================

IHTISHAM UL HAQ MS-22-AM-504821 Page|25


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Task 4.
Write a FORTRAN program to calculate the area of a triangle whose sides a, b and c
using Heron’s relationship.[𝐴𝑟𝑒𝑎 = √𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐) ] , where
[s=(a+b+c)/2]. Also compute the angles opposite to the sides a, b and c, as A, B and C
respectively using the Law of cosines shown in figure. Discuss ten cases of different
values of a, b and c.

Figure 1. Picture taken from onlinemathlearning.com.

Programming Code:
program Lab_1_task_04

real a,b,c,s ! input variables


real Area, A_Angle, B_Angle , C_Angle ! output variables
print*,'Pleas Input sides of triangle a,b,c'
read*,a,b,c
s=(a+b+c)/2.0
area=sqrt(s*(s-a)*(s-b)*(s-c)) ! area through heron's formula
A_Angle=Acos((b**2+c**2-a**2)/(2.0*c*b))
B_Angle= Acos((a**2+c**2-b**2)/(2*a*c))
C_Angle= Acos((a**2+b**2-c**2)/(2*a*b))
print*,' Triangle have the following parameter'
print*,'Area=', area
A_Angle=A_Angle*180.0/3.14
B_Angle=B_Angle*180.0/3.14
C_Angle=C_Angle*180.0/3.14
print*,' A_Angle=', A_Angle ,' B_Angle=', &
B_Angle,' C_Angle=', C_Angle

end program Lab_1_task_04

IHTISHAM UL HAQ MS-22-AM-504821 Page|26


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Screen shot of the Programming code:

Output:

1. Pleas Input sides of triangle a,b,c 4 4 5


Triangle have the following perameter, Area= 7.80624771
A_Angle= 51.3438416 B_Angle= 51.3438416 C_Angle= 77.4036179
2. Pleas Input sides of triangle a,b,c 12 27 20
Triangle have the following parameter, Area= 110.729118
A_Angle= 24.2238903 B_Angle= 112.726761 C_Angle= 43.1406479
3. Pleas Input sides of triangle a,b,c, 10 13 20
Triangle have the following parameter, Area= 56.1465721
A_Angle= 25.6009693 B_Angle= 34.1745491 C_Angle= 120.315788
4. Pleas Input sides of triangle a,b,c, 5 15 20
Triangle have the following parameter, Area= 0.00000000
A_Angle= 0.00000000 B_Angle= 0.00000000 C_Angle= 180.091293
5. Pleas Input sides of triangle a,b,c, 5 5 5
Triangle have the following parameter, Area= 10.8253174
A_Angle= 60.0304298 B_Angle= 60.0304298 C_Angle= 60.0304298

IHTISHAM UL HAQ MS-22-AM-504821 Page|27


PAKISTAN INSTITUTE OF ENGINEERING AND
APPLIED SCIENCES, ISLAMABAD
Computing Fundamentals-I (Lab-I)

Screen shot of output:

=========================End Lab_Task_04===========================

======================Ending of Lab Assignment, No:01===================

IHTISHAM UL HAQ MS-22-AM-504821 Page|28

You might also like