Below is the program to multiply two matrices.
#include <stdio.h>
int main(){
int r,c,k;
printf("Enter no.of rows in first matrix:\n");
scanf("%d",&r);
printf("Enter no.of columns in first matrix:\n");
scanf("%d",&c);
printf("Enter no.of columns in second matrix:\n");
scanf("%d",&k);
int i,j;
int m1[r][c];
int m2[c][k];
int m3[r][k];
printf("Enter values of first matrix:\n");
for(i=0;i<r;++i){
for(j=0;j<c;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%d",&m1[i][j]);
}
}
printf("Enter values of second matrix:\n");
for(i=0;i<c;++i){
for(j=0;j<k;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%d",&m2[i][j]);
}
}
printf("Multiplication of two matrices:\n");
for(i=0;i<r;++i){
for(j=0;j<k;++j){
m3[i][j]=0;
for(int x=0;x<c;++x){
m3[i][j]+=m1[i][x]*m2[x][j];
}printf("%d ",m3[i][j]);
}printf("\n");
}
return 0;
}
Contents
Output:
Enter no.of rows in first matrix:
2
Enter no.of columns in first matrix:
3
Enter no.of columns in second matrix:
2
Enter values of first matrix:
Enter a11: 2
Enter a12: 3
Enter a13: 4
Enter a21: 2
Enter a22: 1
Enter a23: 5
Enter values of second matrix:
Enter a11: 4
Enter a12: 3
Enter a21: 2
Enter a22: 6
Enter a31: 3
Enter a32: 5
Multiplication of two matrices:
26 44
25 37