C Program to find Lower Triangle Matrix

How to write a C Program to find Lower Triangle Matrix with example?. A Lower triangle Matrix is a square matrix where elements above the main diagonal are zeros.

C Program to find Lower Triangle Matrix 0

C Program to find Lower Triangle Matrix Example

This program allows the user to enter the number of rows and columns of a Matrix. Next, this C program will find the Lower triangle of this matrix using For Loop.

#include<stdio.h>

int main()
{
int i, j, rows, columns, a[10][10];

printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);

printf("\n Please Enter the Matrix Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}

for(rows = 0; rows < i; rows++)
{
printf("\n");
for(columns = 0; columns < j; columns++)
{
if(rows >= columns)
{
printf("%d ", a[rows][columns]);
}
else
{
printf("0 ");
}
}
}

return 0;
}

The Matrix lower Triangle output.

 Please Enter Number of rows and columns  :  2 2

 Please Enter the Matrix Elements 
10 20
30 40

10 0  
30 40 

In this C Program to find Lower Triangle Matrix, We declared single Two dimensional arrays Multiplication of size of 10 * 10. The below C Programming statements asks the User to enter the Matrix size (Number of rows and columns. For instance 2 Rows, 2 Columns = a[2][2] )

printf("\n Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

Next, we used for loop to iterate every cell present in a[2][2] matrix. Conditions inside the C for loops ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the 2D array in C limit. Otherwise, the matrix will overflow

scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], …..

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

In the next program line, We have one more for loop to find Lower Triangle of a Matrix

for(rows = 0; rows < i; rows++)
{
	printf("\n");
	for(columns = 0; columns < j; columns++)
    	{
    		if(rows >= columns)
    		{
    			printf("%d ", a[rows][columns]);
		}
		else
		{
			printf("0  ");
		}
 	}
}

User inserted values for C Program to find Lower Triangle Matrix are: a[2][2] = {{10, 20}, { 30, 40}}

Row First Iteration: for(rows = 0; rows < 2; 0++)
The condition (0 < 2) is True.

Column First Iteration: for(columns = 0; 0 < 2; 0++)
The condition (columns < 2) is True. So, it will start executing the statements inside the loop

if(rows >= columns) => if(0 >= 0) – This condition is True. So, it will print a[0][0] = 10

Column Second Iteration: for(columns = 1; 1 < 2; 1++)
The condition (columns < 2) is True.

if(rows >= columns)=> if(0 >= 1) – This condition is False. So, it will print 0

Column Third Iteration: for(columns = 2; 2 < 2; 2++)
The condition (columns < 2) is False. So it will exit from the loop.

Next, rows value will increment to 1.

Row Second Iteration: for(rows = 1; rows < 2; 1++)
The condition (1 < 2) is True.

Column First Iteration: for(columns = 0; 0 < 2; 0++)
The condition (columns < 2) is True. So, it will start executing the statements inside the loop

if(rows >= columns) => if(1 >= 0) – This condition is False. So, it will print a[1][0] = 30

Column Second Iteration: for(columns = 1; 1 < 2; 1++)
The condition (columns < 2) is True.

if(rows >= columns) => if(1 >= 1) – condition is True. So, it will print a[1][1] = 40

Column Third Iteration: for(columns = 2; 2 < 2; 2++)
The condition (columns < 2) is False. So it will exit from the loop.

Next, rows value will increment to 1. After the increment, the condition inside the first for loop (rows < 2) will fail. So it will exit from the loop.

Let me try the C program with the 3 * 3 matrix. Refer to the C multi-dimensional array.

C Program to find Lower Triangle Matrix 2

Also Read

Comments are closed.