Write a C program to take elements of two matrix 3 X 3 and add both matrix.
#include
int main()
{
int mat1[3][3],i,j;
int mat2[3][3],mat3[3][3];
printf("Enter elements of Matrix 1\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter elements of Matrix 2\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat2[i][j]);
}
}
printf("elements of Matrix 1\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t",mat1[i][j]);
}
printf("\n");
}
printf("elements of Matrix 2\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t",mat2[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("Addition of two matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d \t",mat3[i][j]);
}
printf("\n");
}
return 0;
}
Enter elements of Matrix 1
1
2
3
4
5
6
7
8
9
Enter elements of Matrix 2
1
2
4
5
6
7
8
9
elements of Matrix 1
1 2 3
4 5 6
7 8 9
elements of Matrix 2
1 2 3
4 5 6
7 8 9
Addition of two matrix is:
2 4 6
8 10 12
14 16 18
Comments
Post a Comment