# C Program to find theproduct of any two matrices

## C Program to find the product of any two matrices

This page has a C Program to find the product of any two matrices. We can write a C program to find the product of any two matrices. This program will multiply any sizes of two matrices. Here it is remember that, the number of columns of A matrix must be equal to the number of rows of B matrix. If not, the matrix multiplication is not possible. The below given C program is used to find the product of any two matrices. Kindly check out the program to find the product of matrices (matrix multiplication).

### Program for Product of Two Matrices (Matrix multiplication)

#include<stdio.h>
int main(){
int matrixA[10][10], matrixB[10][10], matrixR[10][10];
int r, s, t, m11, m12, m21, m22;
clrscr();
printf("\n Give size of A Matrix : \n");
scanf("%d %d", &m11, &m12);
printf("\n Give size of B Matrix : \n");
scanf("%d %d", &m21, &m22);
if(m12 == m21){
printf("\n Enter Matrix A : \n"); // Read Matrix A
for (r = 1; r <= m11; r++){
for (s = 1; s <= m12; s++){
scanf("%d", &matrixA[r][s]);
}
}
printf("\n Enter Matrix B : \n"); // Read Matrix B
for (r = 1; r <= m21; r++){
for (s = 1; s <= m22; s++){
scanf("%d", &matrixB[r][s]);
}
}
printf("\n The Matrix A is : \n\n"); // Display A Matrix
for (r = 1; r <= m11; r++){
for (s = 1; s <= m12; s++){
printf("\t %d ", matrixA[r][s]);
}
printf("\n");
}
printf("\n The Matrix B is : \n\n"); // Display B Matrix
for (r = 1; r <= m21; r++){
for (s = 1; s <= m22; s++){
printf("\t %d ", matrixB[r][s]);
}
printf("\n");
}

// . . . The two matrices are multipled together . . .

for (r = 1; r <= m11; r++){
for (s = 1; s <= m22; s++){
matrixR[r][s] = 0;
for (t = 1; t <= m12; t++){
matrixR[r][s] = matrixR[r][s]+ matrixA[r][t] * matrixB[t][s];
}
}
}

// . . . Product Matrix is printed in Matrix form . . .

printf("\n\n The Product Matrix is \n\n");
for (r = 1; r <= m11; r++){
for (s = 1; s <= m22; s++){
printf("\t%d", matrixR[r][s]);
}
printf("\n");
}
}
else
printf("\n\n The Matrix sizes are not possible to multiply");
getch();
return (0);
}

Sample Output:

Give size of A Matrix :   4    3

Give size of B Matrix :   3    4

Enter Matrix A :

2       2       2       2       2       2       2       2       2       2       2       2

Enter Matrix B :

2       2       2       2       2       2       2       2       2       2       2       2

The Matrix A is :

2          2         2

2          2         2

2          2         2

2          2          2

The Matrix B is :

2          2         2         2

2          2         2          2

2          2         2         2

The Product Matrix is

12          12           12          12

12          12          12          12

12          12           12          12

/12          12          12          12

Related Popular Posts:

*  Write a c program to check whether a given number is prime number or not?

*  Write a C program to find the prime numbers between 1 and 100

*  Write a C program to display the prime numbers between a range ?

*  Write a C program to find the prime numbers between a range using while loop?

*  Write a C program to find the sum of prime numbers between a range