Program to perform multiplication of two matrices.
Solution in C PYTHON
#include<stdio.h>
int main() {
int mtrx1[10][10], mtrx2[10][10], mtrxMul[10][10];
int row1, col1, row2, col2, i, j, k;
//Read size of first matrix
scanf("%d %d",&row1,&col1);
//Read size of second matrix
scanf("%d %d",&row2,&col2);
//Read matrix1 elements
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
scanf("%d",&mtrx1[i][j]);
for(i=0;i<row2;i++)
for(j=0;j<col2;j++)
scanf("%d",&mtrx2[i][j]);
//To perform multiplication between two matrices
//Number of columns of first matrx is equal to number of rows of second matrix
if(col1==row2){
for(i=0;i<row1;i++){
for(j=0;j<col2;j++){
mtrxMul[i][j] = 0;
for(k=0;k<row2;k++)
mtrxMul[i][j] = mtrxMul[i][j] + mtrx1[i][k]*mtrx2[k][j];
}
}
for(i=0;i<row1;i++){
for(j=0;j<col2;j++){
printf("%d ",mtrxMul[i][j]);
}
printf("\n");
}
}else{
printf("Matrices multiplication is impossible");
}
return 0;
}
int main() {
int mtrx1[10][10], mtrx2[10][10], mtrxMul[10][10];
int row1, col1, row2, col2, i, j, k;
//Read size of first matrix
scanf("%d %d",&row1,&col1);
//Read size of second matrix
scanf("%d %d",&row2,&col2);
//Read matrix1 elements
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
scanf("%d",&mtrx1[i][j]);
for(i=0;i<row2;i++)
for(j=0;j<col2;j++)
scanf("%d",&mtrx2[i][j]);
//To perform multiplication between two matrices
//Number of columns of first matrx is equal to number of rows of second matrix
if(col1==row2){
for(i=0;i<row1;i++){
for(j=0;j<col2;j++){
mtrxMul[i][j] = 0;
for(k=0;k<row2;k++)
mtrxMul[i][j] = mtrxMul[i][j] + mtrx1[i][k]*mtrx2[k][j];
}
}
for(i=0;i<row1;i++){
for(j=0;j<col2;j++){
printf("%d ",mtrxMul[i][j]);
}
printf("\n");
}
}else{
printf("Matrices multiplication is impossible");
}
return 0;
}
Input
2 3
3 2
1 2 3
4 5 6
1 2
3 4
5 6
Output
22 28
49 64
2 3
3 2
1 2 3
4 5 6
1 2
3 4
5 6
Output
22 28
49 64