C Program to find Product of Two Matrices using function

This page has a C Program to find the Product of Two Matrices. It is clear that, this C program will display the product of any Two Matrices. To multiply (find product) any two matrices, the number of columns of the first matrix must be equal to the number of rows of the the second matrix. If the size of A matrix is (5 x 3), and the size of B matrix is (3 x 4), then the two matrices can be multiplied. If the size of A matrix is (4 x 3), and the size of B matrix is (4 x 2), then the two matrices can not be multiplied. For multiplying any two matrices, the two matrices must be compatible. The below given C program will find the product (Multiply) of any Two Matrices. Kindly check out the program to Multiply any Two Matrices.

How will ask to find Product of Two Matrices

The Questions for the matrix multiplication program can be asked as 1). C program to Multiply any Two n X n Matrices. 2). C program to find the product of any Two m X m Matrices. 3). C program to Multiply one matrix with other. 4). C program to find the product of any Two Matrices . 5). C program to Multiply any Two Matrices. 6). C program to find the product of any Two Matrices. 7). C program to Multiply one matrix with other on same size. 8). C program to find the product of any Two square Matrices 9). C program to find the product of Two 3 X 3 Matrices. 10). C program to find product of Two Matrices. 11). C program to multiply Two 3 X 3 Matrices 12). C program to multiply Two Matrices. 13). C program to print the product of Two Matrices. 14). C program to display the product of Two Matrices.

Program for Product of Two Matrices

#include<stdio.h>

void product(int matrix1[][20], int a, int b, int matrix2[][20], int c, int d){

int products[20][20];

int i, j, k;

for (i = 1; i <= a; i++) {

for (j = 1; j <= d; j++) {

products[i][j] = 0;

}}

for (i = 1; i <= a; i++) {

for (j = 1; j <= d; j++) {

for (k = 1; k <= b; k++) {

products[i][j] = products[i][j]+ matrix1[i][k] * matrix2[k][j];

}

}

}

printf("



The Product of Two Matrices is



");

for (i = 1; i <= a; i++) {

for (j = 1; j <= d; j++) {

printf("\t%d", products[i][j]);

}

printf("

");

}

}



int main(){

int m1[20][20], m2[20][20];

int i, j, k, m, n, p, q;

clrscr();

printf(" Enter First Matrix size

");

scanf("%d %d", &m, &n);

printf("

Enter Second Matrix size

");

scanf("%d %d", &p, &q);

if(n==p){

printf("

Enter the elements of First Matrix :

");

for (i = 1; i <= m; i++) {

for (j = 1; j <= n; j++) {

scanf("%d", &m1[i][j]);

}

}

printf("

Enter the elements of Second Matrix:

");

for (i = 1; i <= p; i++) {

for (j = 1; j <= q; j++) {

scanf("%d", &m2[i][j]);

}

}

printf("

The First Matrix is :



");

for (i = 1; i <= m; i++) {

for (j = 1; j <= n; j++) {

printf("\t %d ", m1[i][j]);

}

printf("

");

}

printf("

The Second Matrix is :



");

for (i = 1; i <= p; i++) {

for (j = 1; j <= q; j++) {

printf("\t %d ", m2[i][j]);

}

printf("

");

}

product(m1, m, n, m2, p, q);

}

else

printf("



The Two Matrices are not compatible to find Product !!



");

getch();

return(0);

} Sample Output: Enter First Matrix size

4 3



Enter Second Matrix size

3 4



Enter the elements of First Matrix :



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





Enter the elements of Second Matrix:



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





The First Matrix is :



2 2 2



2 2 2



2 2 2



2 2 2





The Second Matrix is :



2 2 2 2



2 2 2 2



2 2 2 2



The Product of Two Matrices is



12 12 12 12



12 12 12 12



12 12 12 12



12 12 12 12

Related Popular Posts: