matrix multiplication in C Language | C Program to show matrix multiplication | matrix multiplication assignment in C | Multiplication of matrix in C programming
#include
main()
{
int j,i,k,m1,n1,m2,n2;
int A[20][25],B[20][25],mul[20][25]; //declaration of 2 dimensional array
printf("This is a program to get multiplication of two matrices A nad B\n\n");
printf("Enter the rows in mat A..\n");
scanf("%d",&m1);
printf("Enter the columns in mat A..\n");
scanf("%d",&n1);
printf("Now keep on entering matrix elements.\n\n");
for (i=0;i<m1;i++)
{
for (j=0;j<n1;j++)
{
printf("A%d%d=",i+1,j+1);
scanf("%d",&k);
A[i][j]=k;
}
}
printf("\nEnter the rows in mat B..\n");
scanf("%d",&m2);
if(n1!=m2)
{
printf("\n\nDimension error.\nCan not multiply\nexiting...\n");
exit(0);
}
printf("Enter the columns in mat B..\n");
scanf("%d",&n2);
printf("Now keep on entering matrix elements.\n\n");
for (i=0;i<m2;i++)
{
for (j=0;j<n2;j++)
{
printf("B%d%d=",i+1,j+1);
scanf("%d",&k);
B[i][j]=k;
}
}
printf("\n\nMatrix A is\n\n");
for (i=0;i<m1;i++)
{
for (j=0;j<n1;j++)
{
printf ("%d ",A[i][j]);
}
putchar('\n');
}
printf("\n\nMatrix B is\n\n");
for (i=0;i<m2;i++)
{
for (j=0;j<n2;j++)
{
printf ("%d ",B[i][j]);
}
putchar('\n');
}
for (i=0;i<m1;i++) //initialisation of mat mul
{
for (j=0;j<n2;j++)
{
mul[i][j]=0;
}
}
for (i=0;i<m1;i++) //multiplication part
{
for (j=0;j<n2;j++)
{
for (k=0;k<n1;k++)
{
mul[i][j]+=A[i][k]*B[k][j];
}
}
}
printf("\n\nThe multiplied matrix is\n\n");
for (i=0;i<m1;i++)
{
for (j=0;j<n2;j++)
{
printf ("%d ",mul[i][j]);
}
putchar('\n');
}
getchar();
}
Related Links :
Thanks for the code. ccie bootcamp
ReplyDelete