# 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

*and declare a list*

**i***to store the input given by the user row-wise.*

**row**Here we are iterating

*times as to take input for*

**m***columns.*

**m**Implement another * for *loop inside the main for loop over the

*and iterate as a variable*

**range(m)***to store*

**j***elements in a row.*

**m**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

*. Below is our Python code:*

**matrix**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

*as variable*

**range(1, m)***. Implement another*

**i***loop inside the main*

**for***loop over the*

**for***as*

**range(i)***.*

**j**Check whether the element

*not equals zero, if so change the*

**matrix[i][j]***value to 1. This ends the logic for finding a non zero value below the principal diagonal.*

**flag**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:

- Find maximum sum of all diagonals in matrix Python
- How to check whether matrix is a singular or not in Python

## Leave a Reply