Computers in Engineering WWW Site - Example 4.2

Example 4.2


FORTRAN Version

!
      PROGRAM P42
!
!
      IMPLICIT NONE
      INTEGER :: KS,LT,I,J
      PRINT *, 'This is Program >> P42  - Nested DO loops'
!
!     Two nested DO loops
      KS=0
      LT=0
L1:   DO  I=1,20,2
L2:      DO  J=I,25
            KS=KS+J
         END DO L2
         LT=LT+I
         PRINT *,I,KS,LT
      END DO L1
      STOP
      END PROGRAM P42
OUTPUT:

              +--------------------------------------------------+
              |     32-bit Power for Lahey Computer Systems      |
              |   Phar Lap's 386|DOS-Extender(tm) Version 7.0    |
              |  Copyright (C) 1986-94 Phar Lap Software, Inc.   |
              |           Available Memory = 14880 Kb            |
              +--------------------------------------------------+


This is Program >> P42  - Nested DO loops
           1         325           1
           3         647           4
           5         962           9
           7        1266          16
           9        1555          25
          11        1825          36
          13        2072          49
          15        2292          64
          17        2481          81
          19        2635         100

Pascal Version

PROGRAM p42 (input, output);
VAR
  i, j : INTEGER;
  ks, lt : REAL;
BEGIN
  ks := 0;
  lt := 0;
  i := 1;
  WHILE ( i <= 100 ) DO
    BEGIN
      FOR j := 1 to 100 DO
        ks := ks + j;
      lt := lt + i;
      writeln ( i, ' ', ks, ' ', lt );
      i := i + 2
    END { end while }
END.

Last modified: 21/07/97