136. C Program to Find Upper and Lower Triangle Elements of A Matrix.
How does this program work?
- In this programme you will learn about how to find upper triangle and lower triangle elements of matrix using C Language.
- variables are stored in array.
- And then by using the for loop find the elements.
Here is the code
#include <stdio.h>
int main(void)
{
int i,j,rows,col;
printf("Enter number of rows\n");
scanf("%d %d",&rows,&col);
int a[rows][col];
if(col==rows)
{
//Taking input for matrix
printf("Enter Matrix 1\n");
for(i=0;i< rows;i++)
{
for(j=0;j< col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The given Matrix is\n");
for(i=0;i< rows;i++)
{
for(j=0;j< col;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Upper Triangle of Elements are \n");
for(i=0;i< rows;i++)
{
for(j=0;j< col;j++)
{
if(i< j)
{
printf("%d\n",a[i][j]);
}
}
}
printf("Lower Triangle of Elements are \n");
for(i=0;i< rows;i++)
{
for(j=0;j< col;j++)
{
if(i>j)
{
printf("%d\n",a[i][j]);
}
}
}
}
else
{
printf("Not Possible\n");
}
return 0;
}