#include<stdio.h> int main() { int i,j,k,m,n,p,q,sum; printf("Enter Rows and Columns of Matrix 1:"); scanf("%d%d",&m,&n); printf("Enter Rows and Columns of Matrix 2:"); scanf("%d%d",&p,&q); int a[m][n],b[p][q],c[m][q]; if(n != p){ printf("Can't Multiply Matrices"); } else{ printf("Enter Matrix 1:\n"); for(i=0;i<m;i++){ for(j=0;j<n;j++){ scanf("%d",&a[i][j]); } } printf("Enter Matrix 2:\n"); for(i=0;i<p;i++){ for(j=0;j<q;j++){ scanf("%d",&b[i][j]); } } printf("Matrix 1:\n"); for(i=0;i<m;i++){ for(j=0;j<n;j++){ printf("%d\t",a[i][j]); } printf("\n"); } printf("Matrix 2:\n"); for(i=0;i<p;i++){ for(j=0;j<q;j++){ printf("%d\t",b[i][j]); } printf("\n"); } for(i=0;i<m;i++){ for(j=0;j<q;j++){ sum = 0; for(k=0;k<m;k++){ sum = sum + a[i][k] * b[k][j]; } c[i][j] = sum; } } printf("Matrix 3:\n"); for(i=0;i<m;i++){ for(j=0;j<q;j++){ printf("%d\t",c[i][j]); } printf("\n"); } } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter