Tutor Chetan
C Programming Language
Question: Matrix Multiplication
/* Matrix Multiplication */
/* Program Coded By Chetan Thapa Magar */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int m, n, p, q, i, j, k, a[10][10], b[10][10], c[10][10];
printf("Enter the order of A matrix (m*n):\n");
scanf("%d%d", &m, &n);
printf("\nEnter the order of B matrix (p*q):\n");
scanf("%d%d", &p, &q);
if(n==p)
{
printf("\nMatrix can be multiplied.\n");
printf("\nEnter the value of matrix A:\n");
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the value of matrix B:\n");
for(i=0; i<p; i++)
for(j=0; j<q; j++)
scanf("%d", &b[i][j]);
for(i=0; i<m; i++)
for(j=0; j<m; j++)
{
c[i][j]=0;
for(k=0; k<n; k++)
c[i][j]=c[i][j]+a[i][j]*b[k][j];
}
printf("\nThe resultant matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<q; j++)
printf("\t%d", c[i][j]);
printf("\n");
}
}
else
{
printf("\nMatrix multiplication not possible");
printf("\nColumn of A should be equal to row of B\n");
}
getch();
}
Output / Execution:
Enter the order of A matrix (m*n):
2
2
Enter the order of B matrix (p*q):
2
2
Matrix can be multiplied.
Enter the value of Matrix A:
1
3
5
7
Enter the value of Matrix B:
7
5
3
1
The resultant matrix is:
10 18
50 42
2
2
Enter the order of B matrix (p*q):
2
2
Matrix can be multiplied.
Enter the value of Matrix A:
1
3
5
7
Enter the value of Matrix B:
7
5
3
1
The resultant matrix is:
10 18
50 42
0 Comments