# Python Program to check if an input matrix is Upper Triangular

Hey Coder! In this article, we will write a Python program to check if an input matrix is an Upper Triangular matrix.

## Upper Triangular matrix

An Upper Triangular Matrix is a square matrix that has zero as its entries below the principal diagonal.

Example :
1  2  3
0  4  5
0  0  6

## Program

Let us first take the input of the number of rows or columns from the user and store in the variable m. As an Upper Triangular Matrix is a Square matrix, the number of rows is equal to the number of columns.

```m = int(input("Enter the number of rows/columns in the matrix : "))
matrix = list()```

Now declare a list matrix to store the elements of the input matrix.

### Taking Input and Printing the Matrix

Now iterate the for loop over the range(m) as i and declare a list row to store the input given by the user row-wise.
Here we are iterating m times as to take input for m columns.

Implement another for loop inside the main for loop over the range(m) and iterate as a variable j to store m elements in a row.

Now use a print statement to display the element user should enter.

Take input from the user and append it to the list row as an integer. With this statement, we end the inner for loop.

As we need to add each row to the matrix, append each row to the matrix. Below is our Python code:

```for i in range(m):
row = list()
for j in range(m):
print(f"Enter an Element at [{i}][{j}]",end = " ")
row.append(int(input()))
matrix.append(row)```

Now, let us print the input elements in the format of a matrix.

```for row in matrix:
for ele in row:
print(ele, end = "  ")
print()```

### Checking if the Matrix is Upper Triangular

We know that a matrix cannot be Upper Triangular Matrix if at least one element below the principal diagonal equals to a non zero value.

Here, we are going to make use of a variable flag to know if all the elements below the principal diagonal equals zero.

Initialize the flag to 0.

Now, Implement a for loop to iterate over the range(1, m) as variable i. Implement another for loop inside the main for loop over the range(i) as j.
Check whether the element matrix[i][j] not equals zero, if so change the flag value to 1. This ends the logic for finding a non zero value below the principal diagonal.

```flag = 0
for i in range(1, m):
for j in range(i):
if matrix[i][j] != 0:
flag = 1```

Now, use a print statement to display that the matrix is an Upper Triangular if flag equals 0, else display that the matrix is not an Upper Triangular Matrix.

```if flag == 0:
print("Upper Triangular Matrix")
else:
print("Not an Upper Triangulax Matrix")```

### Input & Output

1.

```Enter the number of rows/columns in the matrix : 3
Enter an Element at [0][0] 1
Enter an Element at [0][1] 2
Enter an Element at [0][2] 3
Enter an Element at [1][0] 0
Enter an Element at [1][1] 2
Enter an Element at [1][2] 3
Enter an Element at [2][0] 0
Enter an Element at [2][1] 0
Enter an Element at [2][2] 3
1 2 3
0 2 3
0 0 3
Upper Triangular Matrix```

2.

```Enter the number of rows/columns in the matrix : 2
Enter an Element at [0][0] 2
Enter an Element at [0][1] 1
Enter an Element at [1][0] 1
Enter an Element at [1][1] 3
2 1
1 3
Not an Upper Triangulax Matrix```

Yahoo! In this article, we have learned to take the matrix as an input, print a matrix and also to check if the matrix is an Upper Triangular Matrix in Python program.

Thank You for Reading the article. I hope it helped you someway. Also do check out other related articles to Matrices  below: