Multiply Two 2x2 Matrices in C
Problem: Write a program to multiply two 2x2 matrices.
đ Explanation:
- Multiply matrices A and B to produce matrix C.
- Each element of C is the sum of products of corresponding row of A and column of B.
- Use nested loops for input and multiplication.
✅ C Program:
#include <stdio.h>
int main() {
int A[2][2], B[2][2], C[2][2];
printf("Enter elements of matrix A (2x2):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter elements of matrix B (2x2):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &B[i][j]);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
C[i][j] = 0;
for (int k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
printf("Product of matrix A and B is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
đ¤ Sample Output:
Product of matrix A and B is: 19 22 43 50
āĻোāύ āĻŽāύ্āϤāĻŦ্āϝ āύেāĻ:
āĻāĻāĻি āĻŽāύ্āϤāĻŦ্āϝ āĻĒোāϏ্āĻ āĻāϰুāύ