Page

C program for matrix multiplication

#include<stdio.h>
#include<conio.h>
int main()
{
int a[10][10], b[10][10], c[10][10],m,n, i, j, k, l,g;
printf("Enter the number of rows and column of first matrix\n");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of first %dx%d matrix\n",m,n);
for (i=0; i< m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&a[i][j]);
}
}
q1:printf("\nEnter the number of rows and column of second matrix\n");
scanf("%d%d",&g,&l);
if(n!=g)
{
printf("\nIn matrix multiplication first column and second row number should be the same \n ");
getch();
goto q1;
}
printf("\nEnter the elements of second %dx%d matrix",n,l);
for(i = 0; i <n; i++)
{
for (j = 0; j < l; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("\nThe first matrix is -\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("\t%d", a[i][j]);
}
printf("\n");
}
printf("\nThe second matrix is -\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < l; j++)
{
printf("\t%d", b[i][j]);
}
printf("\n");
}
printf("\nMultiplication of the two matrices \n");
for (i = 0;i < m; i++)

printf("\n");
for (j = 0; j < l; j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j] = c[i][j]+a[i][k] * b[k][j];
printf("\t%d", c[i][j]);
}
}
getch();
}

No comments:

Post a Comment