C program for Multiplication of Two matrices
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10][10], b[10][10], c[10][10];
int r1, c1, r2, c2, i, j, k;
printf("\nEnter rows and cols of Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("\nEnter rows and cols of Matrix B: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2)
{
printf("\nMatrix multiplication not possible");
exit(0);
}
printf("\nEnter elements of Matrix A:\n");
for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\nEnter elements of Matrix B:\n");
for(i = 0; i < r2; i++)
{
for(j = 0; j < c2; j++)
{
scanf("%d", &b[i][j]);
}
}
/* Matrix Multiplication */
for(i = 0; i < r1; i++)
{
for(j = 0; j < c2; j++)
{
c[i][j] = 0;
for(k = 0; k < c1; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
printf("\nMultiplication of matrices:\n");
for(i = 0; i < r1; i++)
{
printf("\n");
for(j = 0; j < c2; j++)
{
printf("%d\t", c[i][j]);
}
}
return 0;
}
Comments
Post a Comment