Python program to check if a given matrix is an upper triangular has been shown here. A square matrix is considered an upper triangular matrix if all the elements below the main diagonal are Zero. The elements on the main diagonal or above may or may not be 0.

Examples:

In the given examples, (i), (ii), (iv) and (v) are upper triangular matrices as every element below the principal diagonal is 0 in each of these matrices. The matrix shown in (iii) is not an upper triangular matrix as one element above the principal diagonal is non-zero. The dotted lines have been drawn over the principal diagonals.

## 1. Algorithm to Check If a Matrix Is an Upper Triangular Matrix

1. Take a matrix $A_{m\times n}$ as input

2. Check if $m=n$

3. If step [2] is false then display "Input matrix is not a square matrix!" and exit program

4. If step [2] is true, then

5. Check if $A_{i,j} \neq 0$ for each $i > j$ and $i \in [1, m]$ and $j \in [1, i]$

6. If step [5] is true for atleast one $A_{i,j}$ then display "The matrix is not an upper triangular matrix" and exit program.

7. If step [5] is false then display "The matrix is an upper triangular matrix" and exit program.

## 2. Pseudocode to Check If a Matrix Is an Upper Triangular Matrix

Input: A matrix $A_{m\times n}$

Output: A is upper triangular or not

1. Procedure upperTriangularMatrix($A_{m\times n}$):

2. If $m == n$:

3. Repeat for each $i > j$ where $i \in [1, m]$ and $j \in [1, i]$

4. If $A_{i,j} \neq 0$:

5. Return Not an upper triangular matrix

6. Return An upper triangular matrix

7. Else:

8. Return It should be a square matrix

9. End Procedure

## 3. Time Complexity to Check If a Matrix Is an Upper Triangular Matrix

Time Complexity: O($mn$)

Where $m$ is the number of rows and $n$ is the number of columns in the matrices.

## 4. Python Program to Check If a Matrix Is an Upper Triangular Matrix

Code has been copied
# **************************************
#        alphabetacoder.com
# Python Program to check if a matrix
#   is an upper triangular matrix
# **************************************

# function to check upper triangular matrix
def checkUpperTriangularMatrix(A):
# get the size of matrix
m = len(A)
n = len(A[0])

# check if the matrix is a square matrix or not
# if it is square matrix, check if it is upper triangular or not
if m != n:
print("Input matrix is not a square matrix!")
else:
# initialize
flag = 0
# check if A is upper triangular or not by
# finding an non-zero element below main diagonal
for i in range(m):
for j in range(i):
if A[i][j] != 0:
# non-zero element found
flag = 1
break

# break the loop as non-zero element
# below main diagonal has been detected
if flag == 1:
break

# display the result
if flag == 0:
print("It is an upper triangular matrix!")
else:
print("It is not an upper triangular matrix!")

def main():
# take input of the order of the matrix
m = int(input("Enter the number of rows of matrix = "))
n = int(input("Enter the number of columns of matrix = "))

# declare a matrix of size m x n
A = [[0] * n for i in range(m)]

# take input of the matrix
for i in range(m):
for j in range(n):
A[i][j] = int(input(f"Enter element at row {i + 1} and column {j + 1}: "))

# display the input matrix
print("Input matrix:")
for i in range(m):
for j in range(n):
print(A[i][j], end=" ")
print("")

# call function to display result
checkUpperTriangularMatrix(A)

# driver code
main()


Output

Enter the number of rows of matrix = 3

Enter the number of columns of matrix = 3

Enter element at row 1 and column 1: 1

Enter element at row 1 and column 2: 2

Enter element at row 1 and column 3: 3

Enter element at row 2 and column 1: 0

Enter element at row 2 and column 2: 4

Enter element at row 2 and column 3: 5

Enter element at row 3 and column 1: 0

Enter element at row 3 and column 2: 0

Enter element at row 3 and column 3: 6

Input matrix:

1 2 3

0 4 5

0 0 6

It is an upper triangular matrix!