Thursday, May 29, 2025
c program to know a matrix is identity or not | matrix examples in c
c program to print sum of two matrices of order mxn | 2D Array-matrix examples
Array programs in C
/* to find sum of two matrix of order mxn
*/
#include<stdio.h>
int main()
{
int m,n,i,j;
printf("enter m as rows and n as columns\n");
scanf("%d%d",&m,&n);//if m=3 then the loop would run for 0,1,2
int m1[m][n],m2[m][n];
printf("enter elements for first matrix m1\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("enter element for location %d %d\n",i,j);
scanf("%d",&m1[i][j]);
}
}
printf("enter elements for second matrix m2\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("enter element for location %d %d\n",i,j);
scanf("%d",&m2[i][j]);
}
}
printf("the sum of two matrices is:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
printf("%d",m1[i][j]+m2[i][j]);
}
printf("\n");
}
return 0;
}
Output:-
c program to print transpose of matrix of order 3x2 | 2D Array-matrix examples
Array programs collections
/* program to find transpose of given matrix of order 3x2
*/
#include<stdio.h>
int main()
{
int matrix[3][2];
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
{
printf("data for %d %d location is:",i,j);
scanf("%d",&matrix[i][j]);
}
}
printf("the matrix before transpose is:\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
{
printf("%d",matrix[i][j]);
}
printf("\n");
}
printf("the matrix after transpose is:\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=2;j++)
{
printf("%d",matrix[j][i]);
}
printf("\n");
}
return 0;
}