Matrix Multiplication in C

Matrix Multiplication in C

Matrix multiplication in C

In this program, I have discussed about how to multiply two matrices using C programming language.
#include<stdio.h>
int main()
{
 int ar1[50][50],ar2[50][50],mul[20][20],i1,i2,j2,j1,k,l,row1,col1,row2,col2,z,m,n,w;
printf("Enter the number of rows of 1st array: \n");
scanf("%d",&row1);
printf("Enter the number of columns in 1st  array: \n");
scanf("%d",&col1);
for(i1=0;i1<row1;i1++)
{
for(j1=0;j1<col1;j1++)
{
printf("\nEnter an element for %dth row and %dth column of 1st array: ",i1,j1);
scanf("%d",&ar1[i1][j1]);
}
}
printf("\nEnter the number of rows of 2nd array: \n");
scanf("%d",&row2);
printf("\nEnter the number of columns in 2nd  array: \n");
scanf("%d",&col2);
for(i2=0;i2<row2;i2++)
{
for(j2=0;j2<col2;j2++)
{
printf("\nEnter an element for %dth row and %dth column of 2nd array: ",i2,j2);
scanf("%d",&ar2[i2][j2]);
}
}
printf("\n");
if(col1!=row2)
{ printf("Matrix multiplication is not possible !!");
}
else
{ for(k=0;k<col1;k++)
{
for(l=0;l<col1;++l)
{ mul[k][l]=0;
for(w=0;w<col1;w++)
{
mul[k][l]+=ar1[k][w]*ar2[w][l];
}

}
}

for(m=0;m<row1;m++)
{
for(n=0;n<col2;n++)
{ printf("%d  ",mul[m][n]);
}
printf("\n");
}
}
}


Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.
Now you can do Matrix multiplication in C.

 

Copyright 2020 Programming in C All Right Reserved