Pages

Sunday, 5 July 2015

Lower triangular matrix

Lower triangular matrix in c

Coding:

#include<stdio.h>
#include<stdlib.h>
int main()
{
int **a,row,col,i,j;
printf("Please enter the total number of rows & columns of squre matrix:");
scanf("%d%d",&row,&col);
if(row==col)
{
a=(int**)malloc(row*sizeof(int));
for(i=0;i<row;i++)
a[i]=(int*)malloc(col*sizeof(int));
printf("Please enter the elements:\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
printf("Matrix elements before transforme:");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
{
printf("%d ",a[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(j>i)
a[i][j]=0;
}
}
printf("\nMatrix elements in lower triangular form:");
for(i=0;i<row;i++)
{
printf("\n");
for(j=0;j<col;j++)
{
printf("%d ",a[i][j]);
}
}
}
else
printf("Operation is not possible");
return 0;
}

Output:
Please enter the total number of rows & columns:3 3
Please enter the elements:
1 2 3
4 5 6
7 8 9
Matrix elements before transforme:
1 2 3
4 5 6
7 8 9
Matrix elements in lower triangular form:
1 0 0
4 5 0
7 8 9