first matrix: second matrix: answer matrix is:
2 3 3 4 30 29
3 2 8 7 27 26
below is code in c language:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
int main()
{
int row1, row2, col1, col2, sum;
printf("Enter the row and column for matrix 1\n");
scanf("%d %d", &row1, &col1);
printf("Enter the row and column for matrix 2\n");
scanf("%d %d", &row2, &col2);
int mat1[row1][col1];
int mat2[row2][col2];
int ansmat[row1][col2];
printf("Enter the first matrix\n");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter the second matrix\n");
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
scanf("%d", &mat2[i][j]);
}
}
if (col1 == row2)
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
sum = 0;
for (int k = 0; k < row2; k++)
{
sum = sum + (mat1[i][k] * mat2[k][j]);
}
ansmat[i][j] = sum;
}
}
printf("The answer matrix is below\n");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
printf("%d\t", ansmat[i][j]);
}
printf("\n");
}
}
else
{
printf("Multiplication is not possible\n");
}
return 0;
}
Comments
Post a Comment