C program to find upper triangular matrix

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.

Upper triangular matrix

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 Aij = 0 (Where ij). 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 😉

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols. as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>