The Tower of Babel

FORTRAN Example

Square Root Algorithm

   PROGRAM MAIN
   INTEGER N
   REAL ABOVE, BELOW, MIDDLE, TOLERANCE
   READ (*,*) N, TOLERANCE
   ABOVE = N
   BELOW = 1
60 MIDDLE = (ABOVE + BELOW)/2
   SQUARE = MIDDLE ** 2
   IF (SQUARE .GT. N) THEN
       ABOVE = MIDDLE
   ELSE
       BELOW = MIDDLE
   ENDIF
   IF(ABS(SQUARE-N) .GT. TOLERANCE) GOTO 60
   WRITE(*,40) N, MIDDLE
40 FORMAT(' ','The square root of ',I7,N  ' is approximately ', F7.2)
   END
[TOC][Next]

CS1104 Main Page
Last Updated 01/05/2000
© L.Heath, 2000