C Program to Multiply Two Matrices

In this source code example, we will write a C program to multiply two matrices using a two-dimensional array.

C Program to Multiply Two Matrices

C Program to Multiply Two Matrices

#include<stdio.h>

void main() {
	int i, j, m, n, p, q, k;
	int a[10][10], b[10][10], c[10][10];
	printf("\nEnter no of rows and column of matrixA:");
	scanf("%d%d", &m, &n);
	printf("\nEnter no of rows and column of matrixB:");
	scanf("%d%d", &p, &q);
	printf("\n Enter elements of matrix A:\n");
	for (i = 0; i < m; i++)
		for (j = 0; j < n; j++)
			scanf("%d", &a[i][j]);
	printf("\n Enter elements of matrix B:\n");
	for (i = 0; i < p; i++)
		for (j = 0; j < q; j++)
			scanf("%d", &b[i][j]);
	if (n == p) {
		for (i = 0; i < m; i++)
			for (j = 0; j < q; j++) {
				c[i][j] = 0;
				for (k = 0; k < n; k++)
					c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
			}
	} else {
		printf("\n Matrix cannot be multiplied");
	}
	printf("\n Display matrix A:\n");
	for (i = 0; i < m; i++) {
		for (j = 0; j < n; j++)
			printf("%d\t", a[i][j]);
		printf("\n");
	}
	printf("\n Display matrix B:\n");
	for (i = 0; i < p; i++) {
		for (j = 0; j < q; j++)
			printf("%d\t", b[i][j]);
		printf("\n");
	}
	printf("\n Display Product:\n");
	for (i = 0; i < m; i++) {
		for (j = 0; j < q; j++)
			printf("%d\t", c[i][j]);
		printf("\n");
	}
}

Output:


Enter no of rows and column of matrixA:
2
2

Enter no of rows and column of matrixB:
2
2

 Enter elements of matrix A:
1
2
2
3

 Enter elements of matrix B:
3
2
1
4

 Display matrix A:
1	2	
2	3	

 Display matrix B:
3	2	
1	4	

 Display Product:
5	10	
9	16	




Comments