To subtract two matrices in C programming, you have to ask
to the user to enter the two matrix, then start subtracting the matrices i.e.,
subtract matrix second from matrix first like matrix1[0][0]-matrix2[0][0], matrix[1][1]-matrix2[1][1],
and so on. Store the result in the third matrix say matrix3[0][0], matrix3[1][1], and
so on.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], i, j, matrix3[3][3];
printf("Enter matrix 1 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix1[i][j]);
}
}
printf("Enter matrix 2 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix2[i][j]);
}
}
printf("Subtracting array (array1-array2) ... \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
matrix3[i][j]=matrix1[i][j]-matrix2[i][j];
}
}
printf("Result of Array1 - Array2 is :\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",matrix3[i][j]);
}
printf("\n");
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int matrix1[3][3], matrix2[3][3], i, j, matrix3[3][3];
printf("Enter matrix 1 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix1[i][j]);
}
}
printf("Enter matrix 2 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&matrix2[i][j]);
}
}
printf("Subtracting array (array1-array2) ... \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
matrix3[i][j]=matrix1[i][j]-matrix2[i][j];
}
}
printf("Result of Array1 - Array2 is :\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ",matrix3[i][j]);
}
printf("\n");
}
getch();
}
Run:
Previous Next
No comments:
Post a Comment