You are on page 1of 2

PROGRAM 7- To add and subtract two matrices

#include<stdio.h>
int main()
{
int i, j, r,c;
int a[5] [5], b[5] [5], add [5] [5], sub [5] [5];
printf("Enter the order of matrix A and B : \n");
scanf("%d%d", &r,&c);
printf("Enter the %d elements of matrix A : \n",r * c);
for (i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &a[i] [j]);
}
}
printf("Enter the %d elements of matrix B : \n", r* c);
for(i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
scanf("%d", &b[i] [j]);
}
}
for (i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
add[i] [j] = a[i] [j] + b[i] [j];
sub[i] [j] = a[i] [j] - b[i] [j];
}
}
printf("Addition of two matrices is : \n");
for(i = 0; i < r; i++)
{
for(j=0; j < c; j++)
{
printf("%4d", add[i] [j] );
}
printf("\n");
}
printf("Subtraction of two matrices is : \n");
for (i = 0; i < r; i++)
{
for(j = 0; j < c; j++)
{
printf("%4d", sub[i][j]);
}
printf("\n");
}
return 0;
}

You might also like