Last active
March 25, 2018 14:21
-
-
Save saifsmailbox98/5253f5139fc69bcd6e7861da45c77424 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| void main() { | |
| int a[50][50], b[50][50], c[50][50], m, n, p, q, sum; | |
| // User input for matrix A | |
| printf("Enter the number of rows for matrix A:"); | |
| scanf("%d", & m); | |
| printf("Enter the number of columns for matrix A:"); | |
| scanf("%d", & n); | |
| // User input for matrix B | |
| printf("Enter the number of rows for matrix B:"); | |
| scanf("%d", & p); | |
| printf("Enter the number of columns for matrix B:"); | |
| scanf("%d", & q); | |
| //Condition for multiplication of matrices | |
| if (n == p) { | |
| // User input for elements of matric A | |
| printf("Enter the elements in matrix A.\n"); | |
| for (int i = 0; i < m; i++) { | |
| for (int j = 0; j < n; j++) { | |
| printf("Enter the values for the element in row %d and column %d: ", i + 1, j + 1); | |
| scanf("%d", & a[i][j]); | |
| } | |
| } | |
| // User input for elements of matric B | |
| printf("Enter the elements in matrix B.\n"); | |
| for (int i = 0; i < p; i++) { | |
| for (int j = 0; j < q; j++) { | |
| printf("Enter the values for the element in row %d and column %d: ", i + 1, j + 1); | |
| scanf("%d", & b[i][j]); | |
| } | |
| } | |
| //Multiplication of the matrices | |
| for (int i = 0; i < m; i++) { | |
| for (int j = 0; j < q; j++) { | |
| sum = 0; | |
| for (int k = 0; k < n; k++) { | |
| sum += a[i][k] * b[k][j]; | |
| } | |
| c[i][j] = sum; | |
| } | |
| } | |
| //Printing matrix A | |
| printf("\nMatrix A:\n"); | |
| for (int i = 0; i < m; i++) { | |
| for (int j = 0; j < n; j++) { | |
| printf("%d ", a[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| //Printing matrix B | |
| printf("\nMatrix B:\n"); | |
| for (int i = 0; i < p; i++) { | |
| for (int j = 0; j < q; j++) { | |
| printf("%d ", b[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| //Printing the resultant matrix | |
| printf("\nResultant matrix:\n"); | |
| for (int i = 0; i < m; i++) { | |
| for (int j = 0; j < q; j++) { | |
| printf("%d ", c[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| } else { | |
| //Error message if condition is not met for multiplication | |
| printf("The matrices cannot be multiplied."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment