Python Program to Display Upper Triangular Matrix

A matrix is a two-dimensional array of numbers arranged in rows and columns. A square matrix (whose rows and columns have the same number of elements) has two diagonals. One is the primary diagonal located from the top left corner to the bottom right corner. The second is the secondary diagonal located from the top right to the bottom left corner.

For a square matrix, if all the elements below the primary diagonal are zero, then it is called the Upper Triangular Matrix.

# Example of upper triangular matrix
matrix = [[1, 3, 4],
          [0, 5, 6],
          [0, 0, 3]]

for row in matrix:
    print(row)
[1, 3, 4]
[0, 5, 6]
[0, 0, 3]

A matrix cannot be converted to an upper triangular matrix if the given matrix is not a square matrix.

Input Output Scenario

Consider a 4×4 square matrix that we want to convert to upper triangular form ?

Input matrix:
[1, 3, 5, 7]
[9, 2, 4, 2]
[6, 3, 1, 4]
[5, 8, 7, 6]
 
Upper triangular matrix:
[1, 3, 5, 7]
[0, 2, 4, 2]
[0, 0, 1, 4]
[0, 0, 0, 6]

Method 1: Displaying with Zeros

In this approach, we replace lower triangle elements with zeros while displaying ?

matrix = [[1, 2, 3],
          [4, 5, 6],
          [1, 8, 5]]

print("Original matrix:")
for row in matrix:
    print(row)

print("\nUpper triangular matrix:")

# Check if matrix is square
if len(matrix) != len(matrix[0]):
    print("Matrix should be a square matrix")
else:
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
            if i <= j:
                print(matrix[i][j], end="  ")
            else:
                print(0, end="  ")
        print()
Original matrix:
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

Upper triangular matrix:
1  2  3  
0  5  6  
0  0  5  

Method 2: Displaying Only Upper Elements

This method displays only the upper triangular elements, leaving spaces for lower elements ?

matrix = [[1, 2, 3],
          [4, 5, 6],
          [1, 8, 5]]

print("Original matrix:")
for row in matrix:
    print(row)

print("\nUpper triangular matrix:")
for i in range(len(matrix)):
    for j in range(len(matrix[0])):
        if i > j:
            print("  ", end="")
        else:
            print(matrix[i][j], end=" ")
    print()
Original matrix:
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

Upper triangular matrix:
1 2 3 
  5 6 
    5 

Method 3: Modifying Original Matrix

This approach permanently modifies the original matrix by setting lower triangle elements to zero ?

matrix = [[1, 2, 3],
          [4, 5, 6],
          [1, 8, 5]]

print("Original matrix:")
for row in matrix:
    print(row)

print("\nConverting to upper triangular matrix:")
for i in range(len(matrix)):
    for j in range(len(matrix[0])):
        if i > j:
            matrix[i][j] = 0

print("Modified matrix:")
for row in matrix:
    print(row)
Original matrix:
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

Converting to upper triangular matrix:
Modified matrix:
[1, 2, 3]
[0, 5, 6]
[0, 0, 5]

Comparison

Method Modifies Original Best For
Display with Zeros No Preserving original data
Display Only Upper No Visual representation
Modify Original Yes Memory efficiency

Conclusion

Upper triangular matrices have zeros below the main diagonal. Use Method 1 to preserve original data, Method 2 for visual display, or Method 3 when you need to permanently modify the matrix.

Updated on: 2026-03-27T06:41:18+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements