Print Product of Two Matrices

C Program to Print Product of Two Matrices

Following C Program to Print Product of Two Matrices, then display the result on the screen:

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y], you would write something as follows


type arrayName [ x ][ y ];

Where type can be any valid C data type and array Name will be a valid C identifier. A two-dimensional array can be considered as a table which will have x number of rows and y number of columns.

The below A 3 x 4 Table show as per below.

Save program in a file, Compile program, debug errors, Execute or Run program with necessary inputs. Verify the outputs obtained.

Before doing into this topic with examples, users can have knowledge on below,

Array with Example

Example : C Program to Print Product of Two Matrices

/* Program to print product of two matrices*/
#include <stdio.h>
int main()
{
int m, n, p, q, i, j, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{ printf("Enter the elements of first matrix\n");
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
scanf("%d", &first[i][j]);
printf("Enter the elements of second matrix\n");
for (i = 0; i < p; i++)
for (j = 0; j < q; j++)
scanf("%d", &second[i][j]);
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
multiply[i][j]=0;
for (k = 0; k < p; k++) {
multiply[i][j] = multiply[i][j] + first[i][k]*second[k][j];
}
}
}
printf("Product of entered matrices:-\n");
for (i = 0; i < m; i++) {
for (j = 0; j < q; j++)
printf("%d\t", multiply[i][j]);
printf("\n");
}
}
return 0;
}

Output

Enter the number of rows and columns of first matrix 3 3
Enter the number of rows and columns of second matrix 3 3

Enter the elements of first matrix
1 2 4 5 2 1 4 5 2
the elements of second matrix
1 2 4 5 2 1 4 5 2

Product of entered matrices
10 18 28
50 18 7
40 45 14

Let me know if you find any difficulty in understanding this C Program to Print Product of Two Matrices example and I would be glad to explain it further.

 

Online Training Tutorials