Matrix Multiplication in C Language |
|
| 1 | #include <stdio.h> |
| 2 | int main() |
| 3 | { |
| 4 | int m, n, p, q, c, d, k, sum = 0; |
| 5 | int first[10][10], second[10][10], multiply[10][10]; |
| 6 | printf("Enter the number of 'rows' and 'columns' of first matrix\n"); |
| 7 | scanf("%d%d", &m, &n); |
| 8 | printf("Enter the 'elements' of 'first matrix'\n"); |
| 9 | for (c = 0; c < m; c++) |
| 10 | for (d = 0; d < n; d++) |
| 11 | scanf("%d", &first[c][d]); |
| 12 | printf("Enter the number of' rows' and 'columns' of 'second matrix'\n"); |
| 13 | scanf("%d%d", &p, &q); |
| 14 | |
| 15 | if (n != p) |
| 16 | printf("Matrices with entered orders can't be multiplied with each-other.\n"); |
| 17 | else |
| 18 | { |
| 19 | printf("Enter the 'elements' of 'second-matrix'\n"); |
| 20 | |
| 21 | for (c = 0; c < p; c++) |
| 22 | for (d = 0; d < q; d++) |
| 23 | scanf("%d", &second[c][d]); |
| 24 | for (c = 0; c < m; c++) |
| 25 | { |
| 26 | for (d = 0; d < q; d++) |
| 27 | { |
| 28 | for (k = 0; k < p; k++) |
| 29 | { |
| 30 | sum = sum + first[c][k]*second[k][d]; |
| 31 | } |
| 32 | multiply[c][d] = sum; |
| 33 | sum = 0; |
| 34 | } |
| 35 | } |
| 36 | printf("Product of 'entered matrices':\n"); |
| 37 | |
| 38 | for (c = 0; c < m; c++) |
| 39 | { |
| 40 | for (d = 0; d < q; d++) |
| 41 | printf("%d\t", multiply[c][d]); |
| 42 | printf("\n"); |
| 43 | } |
| 44 | } |
| 45 | return 0; |
| 46 | } |
OUT PUT:
