Print the Upper Triangular Part of a Square Matrix

Problem Statement

Suppose we have an input of the following form. The first line gives the number of rows (and columns) of a square matrix. Each of the subsequent input lines contains the values of a row of the matrix in the form of (10I5). Note that if a matrix has fewer than ten rows (and columns), there will be less than 10 numbers on an input line.
         1    1    2    2    3
....5....0....5....0....5....0
6
35   70   52   83   98   68
12   76   35   24   9    27
46   49   53   57   15   29
66   87   25   40   93   21
100  65   77   88   4    51
83   12   19   54   7    100

Write a Fortran program that reads in this matrix and prints the upper triangular part. The upper triangular part of a square matrix consists of all entries on and above the diagonal.

         1    1    2    2    3    3
....5....0....5....0....5....0....5
 Input Matrix:
    35   70   52   83   98   68
    12   76   35   24    9   27
    46   49   53   57   15   29
    66   87   25   40   93   21
   100   65   77   88    4   51
    83   12   19   54    7  100

 Upper Triangular Part:
    35   70   52   83   98   68
         76   35   24    9   27
              53   57   15   29
                   40   93   21
                         4   51
                            100

Solution

PROGRAM  UpperTriangularMatrix
   IMPLICIT   NONE
   INTEGER, PARAMETER                :: SIZE = 10
   INTEGER, DIMENSION(1:SIZE,1:SIZE) :: Matrix
   INTEGER                           :: Number
   INTEGER                           :: Position
   INTEGER                           :: i, j
   CHARACTER(LEN=100)                :: Format

   READ(*,"(I5)")  Number
   DO i = 1, Number
      READ(*,"(10I5)")  (Matrix(i,j), j = 1, Number)
   END DO

   WRITE(*,"(1X,A)")  "Input Matrix:"
   DO i = 1, Number
      WRITE(*,"(1X,10I5)")  (Matrix(i,j), j = 1, Number)
   END DO

   WRITE(*,"(/1X,A)") "Upper Triangular Part:"
   Position = 2
   DO i = 1, Number
      WRITE(Format,"(A,I2.2,A)")  "(T", Position, ", 10I5)"
      WRITE(*,Format)  (Matrix(i,j), j = i, Number)
      Position = Position + 5
   END DO
END PROGRAM  UpperTriangularMatrix
Click here to download this program.

Program Input and Output

If the input data consist of the following:
         1    1    2    2    3
....5....0....5....0....5....0
6
35   70   52   83   98   68
12   76   35   24   9    27
46   49   53   57   15   29
66   87   25   40   93   21
100  65   77   88   4    51
83   12   19   54   7    100

The output of the program is:
         1    1    2    2    3    3
....5....0....5....0....5....0....5
 Input Matrix:
    35   70   52   83   98   68
    12   76   35   24    9   27
    46   49   53   57   15   29
    66   87   25   40   93   21
   100   65   77   88    4   51
    83   12   19   54    7  100

 Upper Triangular Part:
    35   70   52   83   98   68
         76   35   24    9   27
              53   57   15   29
                   40   93   21
                         4   51
                            100

Discussion

Similar to the previous example, this one also looks easy. Without the help of internal files, it could be very difficult.