Using - C
#include<stdio.h>
void main() {
int rows, cols, i,j;
printf("Enter number of rows: ");
scanf("%d",&rows);
printf("Enter number of columns: ");
scanf("%d",&cols);
int a[rows][cols], b[rows][cols], c[rows][cols];
printf("Enter Elements of 1st Matrix: \n");
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
scanf("%d",&a[i][j]);
}
}
printf("Enter Elements of 2nd Matrix: \n");
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
scanf("%d",&b[i][j]);
}
}
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
printf("\n\nDIFFERENCE OF TWO MATRICES:\n");
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
printf("%d ",c[i][j]);
}
printf("\n");
}
}
OUTPUT
Enter number of rows: 2
Enter number of columns: 2
Enter Elements of 1st Matrix:
35 30
28 24
Enter Elements of 2nd Matrix:
10 12
15 20
DIFFERENCE OF TWO MATRICES:
25 18
13 4
Using - C++
#include<iostream>
using namespace std;
int main() {
int rows, cols, i,j;
cout<< "Enter number of rows: ";
cin>> rows;
cout<< "Enter number of columns: ";
cin>> cols;
int a[rows][cols], b[rows][cols], c[rows][cols];
cout<< "Enter Elements of 1st Matrix: \n";
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
cin>> a[i][j];
}
}
cout<< "Enter Elements of 2nd Matrix: \n";
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
cin>> b[i][j];
}
}
for(i=0; i < rows; i++) {
for(j=0; j < cols; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
cout<< "\n\nDIFFERENCE OF TWO MATRICES:\n";
for(i=0; i < rows; i++){
for(j=0; j < cols; j++){
cout<< c[i][j]<< " ";
}
cout<< "\n";
}
}
OUTPUT
Enter number of rows: 3
Enter number of columns: 3
Enter Elements of 1st Matrix:
45 38 42
36 58 68
54 56 70
Enter Elements of 2nd Matrix:
22 30 35
26 25 50
32 23 59
DIFFERENCE OF TWO MATRICES:
23 8 7
10 33 18
22 33 11