Design an algorithm to convert a lower triangular matrix to upper triangular matrix.
#include<stdio.h>
int main()
{
int x, y;
int r,c;
printf("Enter rows\n");
scanf("%d", &r);
printf("Enter columns\n");
scanf("%d", &c);
int mat[r][c];
printf("\nEnter matrix items:\n");
for(x=0;x<r;x++)
{
for(y=0;y<c;y++)
{
scanf("%d", &mat[x][y]);
}
}
printf("\n");
printf("Lower triangular matrix: \n") ;
for (x= 0; x < r; x++)
{
for (y= 0; y < c; y++)
{
if (x < y)
{
printf("0\t");
}
else
printf("%d\t", mat[x][y]);
}
printf("\n");
}
printf("Upper triangular matrix: \n");
for (x = 0; x < r; x++)
{
for (y = 0; y < c; y++)
{
if (x > y)
{
printf("0\t");
}
else
printf("%d\t", mat[x][y]);
}
printf("\n");
}
return 0;
}
Comments
Leave a comment