C program for subtraction of two matrices (2D Arrays)
#include <stdio.h>
#include <stdlib.h>
int main() {
int a[10][10], b[10][10], c[10][10];
int ar, ac, br, bc, i, j;
printf("\nEnter how many rows and cols of Matrix A: ");
scanf("%d %d", &ar, &ac);
printf("\nEnter how many rows and cols of Matrix B: ");
scanf("%d %d", &br, &bc);
if (ar != br || ac != bc) {
printf("\nInvalid Subtraction (Matrices must have the same dimensions)\n");
exit(0);
}
printf("\nEnter the elements of Matrix A:\n");
for (i = 0; i < ar; i++) {
for (j = 0; j < ac; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter the elements of Matrix B:\n");
for (i = 0; i < br; i++) {
for (j = 0; j < bc; j++) {
scanf("%d", &b[i][j]);
}
}
// Subtraction of matrices
for (i = 0; i < ar; i++) {
for (j = 0; j < ac; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
printf("\nSubtraction of Matrix A and Matrix B:\n");
for (i = 0; i < ar; i++) {
printf("\n");
for (j = 0; j < ac; j++)
{
printf("%d\t", c[i][j]);
}
}
return 0;
}
Comments
Post a Comment