Matrix Multiplication in C
In this example, you will get and learn the program code of Matrix multiplication in c using array.
Matrix is the sequence of values arranged in rows and columns using two dimensional array. Many operations, like matrix addition, matrix subtraction, and matrix multiplication, can be performed on the matrix.
Matrix Multiplication how to ?
The following formula explains the method of Matrix Multiplication in C programming.
Example Matrix Multiplication in C using Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<stdio.h> int main() { int a[3][3]; int b[3][3]; int m[3][3]; int rows=0,cols=0; int r=3,c=3,i,j; for(int i=0;i<r;i++) { rows=i+1; for(int j=0;j<c;j++) { cols=j+1; printf("\nEnter the value for %d row %d column ",rows,cols); scanf("%d",&a[i][j]); } } for(i=0;i<r;i++) { rows=i+1; for(int j=0;j<c;j++) { cols=j+1; printf("\nEnter the value for %d row %d column ",rows,cols); scanf("%d",&b[i][j]); } } int r1=0,c1=0,r2=0,c2=0; int p=0; for(i=0;i<r;i++) { for(int j=0;j<c;j++) { p=0; for(int k=0;k<r;k++) { p=p+a[r1][c1]*b[r2][c2]; c1++;r2++; } m[i][j]=p; c1=0;r2=0;c2++; } r1++;c1=0;c2=0;r2=0; } printf("\n***************Matrix after Multiplication***********************\n\n"); for(i= 0;i<r;i++) { for(int j=0;j<c;j++) { printf("%d",m[i][j]); printf(" "); } printf("\n"); } //getch(); } |
Output