Program 5
Program 5
h>
int main()
{
int m,n,a,i,j;
int mat1[10][10];
int mat2[10][10];
int mat3[10][10];
printf("Enter 1 for Addition of two matrices:");
printf("\n");
printf("Enter 2 for Subtraction of two matrices:");
printf("\n");
printf("Enter 3 for finding upper and lower triangular matrix:");
printf("\n");
printf("Enter 4 for transpose of matrix:");
printf("\n");
printf("Enter 5 for Product of two matrices:");
printf("\n");
printf("Enter the number of rows:");
scanf("%d",&m);
printf("Enter the number of columns:");
scanf("%d",&n);
switch(a)
{
case 1:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements of 2nd matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("Addition of two matrices is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
break;
case 2:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements of 2nd matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat3[i][j]=mat1[i][j]-mat2[i][j];
}
}
printf("Subtraction of two matrices is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
break;
case 3:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Upper triangular elements are:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
printf("0 ");
}
else
{
printf("%d\t",mat1[i][j]);
}
}
printf("\n");
}
printf("lower triangular elements are:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i<j)
{
printf("0");
}
else
{
printf("%d\t",mat1[i][j]);
}
}
printf("\n");
}
break;
case 4:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Original matrix is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is:");
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",mat1[j][i]);
}
printf("\n");
}
break;
case 5:
printf("Enter the elements of 1st matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements of 2nd matrix:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat2[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mat3[i][j]=0;
for(int k=0;k<n;k++)
{
mat3[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
printf("product of two matrices is:");
printf("\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat3[i][j]);
}
printf("\n");
}
break;
default:
printf("Enter a valid choice");
break;
}
return 0;
}