Write a C program to read elements in a matrix and check whether the matrix is upper triangular matrix or not. C program to check upper triangular matrix. Logic to find upper triangular matrix in C programming.

**Example**

**Input**

Input elements of matrix: 1 2 3 0 5 6 0 0 9

**Output**

Matrix is upper triangular

## Required knowledge

Basic C programming, For loop, Array

## Upper triangular matrix

Upper triangular matrix is a special square matrix whose all elements below the main diagonal is zero.

## Logic to find upper triangular matrix

To check whether a matrix is upper triangular or not we need to check whether all elements below main diagonal are zero or not.

For any matrix **A** if all elements **A**_{ij} = 0 (Where *i* ≥ *j*). Means, if(array[row][col] == 0) and row > col then it is upper triangular matrix.

Must read - Program to find lower triangular matrix

## Program to find upper triangular matrix

```
/**
* C program to find upper triangular matrix
*/
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
int main()
{
int array[MAX_ROWS][MAX_COLS];
int row, col, isUpper;
/* Input elements in matrix from user */
printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS);
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_COLS; col++)
{
scanf("%d", &array[row][col]);
}
}
/* Check Upper triangular matrix condition */
isUpper = 1;
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_COLS; col++)
{
/*
* If elements below the main diagonal (col<row)
* is not equal to zero then it is not upper triangular matrix
*/
if(col<row && array[row][col]!=0)
{
isUpper = 0;
}
}
}
/* Print elements of upper triangular matrix */
if(isUpper == 1)
{
printf("\nThe matrix is Upper triangular matrix.\n");
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_COLS; col++)
{
printf("%d ", array[row][col]);
}
printf("\n");
}
}
else
{
printf("\nThe matrix is not Upper triangular matrix.");
}
return 0;
}
```

Output

Enter elements in matrix of size 3x3: 1 2 3 0 5 6 0 0 9 The matrix is Upper triangular matrix. 1 2 3 0 5 6 0 0 9

Happy coding 😉

### Recommended posts

- Array and Matrix programming exercises index.
- C program to find sum of main diagonal elements of a matrix.
- C program to find sum of opposite diagonal elements of a matrix.
- C program to find sum of each row and columns of a matrix.
- C program to interchange diagonals of a matrix.
- C program to check symmetric matrix.
- C program to check sparse matrix.

doubt, write here. I willhelp my best.<pre><code> ----Your Source Code---- </code></pre>