// C program to illustrate matrix multiplication
#include <stdio.h>
void main()
{
int r1,r2,c1,c2, i, j,k, first[10][10], second[10][10], mul[10][10];
printf("Enter the number of rows (r1) and columns (c1) of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter the number of rows (r2) and columns (c2) of second matrix\n");
scanf("%d%d", &r2, &c2);
if(c1 == r2)
{
printf("Enter the %d elements of first matrix\n",r1 * c1);
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
scanf("%d", &first[i][j]);
}
}
printf("\nFirst matrix :\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf("%d\t", first[i][j]);
}
printf("\n");
}
printf("Enter the %d elements of second matrix\n",r2 * c2);
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
scanf("%d", &second[i][j]);
}
}
printf("\nSecond matrix : \n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf("%d\t", second[i][j]);
}
printf("\n");
}
printf("Matrix Multiplication is :-\n");
for (i = 0; i < r1; i++)
{
for (j = 0 ; j < c2; j++)
{
mul[i][j]=0;
for (k = 0 ; k < c1; k++)
{
mul[i][j] = mul[i][j] + (first[i][k] * second[k][j]);
}
}
}
for (i = 0; i < r1; i++)
{
for (j = 0 ; j < c2; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication can not perform since r1 is not equal to c2 \n");
}
output:
#include <stdio.h>
void main()
{
int r1,r2,c1,c2, i, j,k, first[10][10], second[10][10], mul[10][10];
printf("Enter the number of rows (r1) and columns (c1) of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter the number of rows (r2) and columns (c2) of second matrix\n");
scanf("%d%d", &r2, &c2);
if(c1 == r2)
{
printf("Enter the %d elements of first matrix\n",r1 * c1);
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
scanf("%d", &first[i][j]);
}
}
printf("\nFirst matrix :\n");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf("%d\t", first[i][j]);
}
printf("\n");
}
printf("Enter the %d elements of second matrix\n",r2 * c2);
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
scanf("%d", &second[i][j]);
}
}
printf("\nSecond matrix : \n");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf("%d\t", second[i][j]);
}
printf("\n");
}
printf("Matrix Multiplication is :-\n");
for (i = 0; i < r1; i++)
{
for (j = 0 ; j < c2; j++)
{
mul[i][j]=0;
for (k = 0 ; k < c1; k++)
{
mul[i][j] = mul[i][j] + (first[i][k] * second[k][j]);
}
}
}
for (i = 0; i < r1; i++)
{
for (j = 0 ; j < c2; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication can not perform since r1 is not equal to c2 \n");
}
output:
No comments:
Post a Comment