P38.F90

Finding the area and perimeter of a triangle

```!
!
!  TRIANGLE PROGRAM P38.F90
!
!  This program will find the perimeter and the area of a triangle
!
!
REAL A,B,C,PERI,SEMI,AREA
!
!    Tell program where data for  READ *  is coming from
OPEN(UNIT=5, FILE='P38.DAT')      ! UNIT=5 is the default input
!
!
!
DO N=1,20
IF( A == -90 ) STOP

IF( A <= 0 .OR. B <= 0 .OR. C <= 0 ) THEN
PRINT 5,A,B,C
PRINT *,'ALL THE VALUES MUST BE POSITIVE.'
5         FORMAT(' A=',F8.3,'  B=',F8.3,' C=',F8.3)
CONTINUE
ELSE
PERI = A+B+C
SEMI = PERI/2
AREA = SEMI * (SEMI-A) * (SEMI-B) * (SEMI-C)
IF (AREA .LT. 0) THEN
PRINT 5,A,B,C
PRINT *,'TWO SIDES SHORTER THAN THIRD'
ELSE
PRINT 8,A,B,C,PERI,SQRT(AREA)
8            FORMAT(' A=',F8.3,' B=',F8.3,' C=',F8.3,' PERIMETER =', &
F8.3,' AREA=',F8.3)
ENDIF
ENDIF
END DO
END

DATA:
5  7  3
5 -2 5
34 62 54
30 5 43
-90 -90 -90

OUTPUT:
Program entered
A=   5.000 B=   7.000 C=   3.000 PERIMETER =  15.000 AREA=   6.495
A=   5.000  B=  -2.000 C=   5.000
ALL THE VALUES MUST BE POSITIVE.
A=  34.000 B=  62.000 C=  54.000 PERIMETER = 150.000 AREA= 916.229
A=  30.000  B=   5.000 C=  43.000
TWO SIDES SHORTER THAN THIRD
Fortran-90 STOP
```

Come back to the previous page

Page builder: Charles Boivin