C program to find lower triangular matrix

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

Example

Input

Input elements in matrix:
1 0 0
4 5 0
7 8 9

Output

Matrix is lower triangular

Required knowledge

Basic C programming, For loop, Array

Lower triangular matrix

Lower triangular matrix is a special square matrix whose all elements above the main diagonal is zero.

Lower triangular matrix

Read more - Program to find upper triangular matrix

Logic to find lower triangular matrix

To find whether a matrix is lower triangular or not we need to check if all elements above main diagonal of the matrix is zero or not.
For any matrix A if elements Aij = 0 (Where j ≥ i). Means, if(array[row][col] == 0) and j > i then it is lower triangular matrix.

Program to find lower triangular matrix

/**
 * C program to find lower triangular matrix
 */

#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3

int main()
{
    int array[MAX_ROWS][MAX_COLS];
    int row, col, isLower;

    /* 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 whether the matrix is lower triangular matrix */
    isLower = 1;
    for(row=0; row<MAX_ROWS; row++)
    {
        for(col=0; col<MAX_COLS; col++)
        {
            /*
             * If elements above main diagonal(col>row)
             * is not equal to zero(array[row][col]!=0)
             */
            if(col>row && array[row][col]!=0)
            {
                isLower = 0;
            }
        }
    }

    /*
     * If matrix is lower triangular matrix
     */
    if(isLower == 1)
    {
        printf("\nMatrix is Lower triangular matrix: \n");

        /* Print elements of lower triangular matrix */
        for(row=0; row<MAX_ROWS; row++)
        {
            for(col=0; col<MAX_COLS; col++)
            {
                printf("%d ", array[row][col]);
            }

            printf("\n");
        }
    }
    else
    {
        printf("\nMatrix is not a Lower triangular matrix");
    }

    return 0;
}

Output

Enter elements in matrix of size 3x3:
1 0 0
4 5 0
7 8 9

Matrix is Lower triangular matrix:
1 0 0
4 5 0
7 8 9

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>