0% found this document useful (0 votes)
46 views2 pages

Program To Multiply Two Matrices

The program multiplies two matrices where the order and elements are input by the user. It checks if the matrices can be multiplied by comparing the number of columns of the first matrix with the number of rows of the second. If multiplication is possible, it inputs the matrices, prints them, and calculates the product matrix using a nested for loop. If multiplication is not possible, it alerts the user.

Uploaded by

rk135
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Program To Multiply Two Matrices

The program multiplies two matrices where the order and elements are input by the user. It checks if the matrices can be multiplied by comparing the number of columns of the first matrix with the number of rows of the second. If multiplication is possible, it inputs the matrices, prints them, and calculates the product matrix using a nested for loop. If multiplication is not possible, it alerts the user.

Uploaded by

rk135
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

www.eazynotes.com Gursharan Singh Tatla Page No.

/*
Program to multiply two matrices. The order and the elements of the
two matrices will be entered by the user as input to the program
and if multiplication is not possible then it should be reported to
the user
*/

#include <stdio.h>

main()
{
int a[10][10], b[10][10], c[10][10], i, j, k, r1, r2, c1, c2;

back:
printf("\nEnter no. of rows and columns of Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("\nEnter no. of rows and columns of Matrix B: ");
scanf("%d %d", &r2, &c2);

if (c1 != r2)
{
printf("\n\nMultiplication is not possible\n");
goto back;
}

printf("\n\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]);
www.eazynotes.com Gursharan Singh Tatla Page No. 2

printf("\n\nElements of Matrix A:\n\n");


for (i=0; i<r1; i++)
{
for (j=0; j<c1; j++)
printf("\t%d", a[i][j]);
printf("\n\n");
}

printf("\n\nElements of Matrix B:\n");


for (i=0; i<r2; i++)
{
for (j=0; j<c2; j++)
printf("\t%d", b[i][j]);
printf("\n\n");
}

for (i=0; i<r1; i++)


for (j=0; j<c2; j++)
{
c[i][j] = 0;
for (k=0; k<r2; k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}

printf("\n\nMultiplication of Matrices:\n\n");
for (i=0; i<r1; i++)
{
for (j=0; j<c2; j++)
printf("\t%d", c[i][j]);
printf("\n\n");
}

getch();
}

You might also like