Array Exercises
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.
Required knowledge
Basic C programming, C for loop, ArrayS
Lower triangular matrix
Lower triangular matrix is a special square matrix whose all elements above the main diagonal is zero.

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:
10 0 0
40 50 0
70 80 90
Matrix is Lower triangular matrix:
10 0 0
40 50 0
70 80 90
Process returned 0 (0x0) execution time : 42.360 s
Enter elements in matrix of size 3x3:
10 20 0
40 50 0
70 80 90
Matrix is not a Lower triangular matrix
Process returned 0 (0x0) execution time : 24.424 s
Program to find lower triangular matrix
/**
* C program to find lower triangular matrix
*/
#include <stdio.h>
int main()
{
int array[10][10];
int row, col, isLower,m,n;
printf("Enter number of rows:");
scanf("%d",&m);
printf("Enter number of columns:");
scanf("%d",&n);
/* Input elements in matrix from user */
printf("Enter elements in matrix of size %dx%d: \n", m, n);
for(row=0; row<m; row++)
{
for(col=0; col<n; col++)
{
scanf("%d", &array[row][col]);
}
}
/* Check whether the matrix is lower triangular matrix */
isLower = 1;
for(row=0; row<m; row++)
{
for(col=0; col<n; 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<m; row++)
{
for(col=0; col<n; col++)
{
printf("%d ", array[row][col]);
}
printf("\n");
}
}
else
{
printf("\nMatrix is not a Lower triangular matrix");
}
return 0;
}
