The program for matrix multiplication in c is to multiply two matrices.

What is matrix: It is a collection of numbers arranged in rows and columns.
Matrix representation
Matrices are stored in the multidimensional array in c which is stored in rows and column format.

How to multiply two matrices
Matrix multiplication: First we take two matrices with specific rows and columns and multiply matrices. And store in the third matrix. Print Multiplication of matrices.
Note:- we can’t multiply matrices with different sizes.

Matrix Multiplication example

Matrix Multiplication

Algorithm for matrix Multiplication in c

  1. Declare variable
  2. Take two matrices input
  3. Multiply matrices by adding it’s pair
  4. Print multiplied matrix

C program for Matrix Multiplication

#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int matrix1[10][10], matrix2[10][10], multiply[10][10];

printf("Enter number of rows and columns of matrix1 \n");
scanf("%d%d", &m, &n);
printf("Enter elements of matrix1 \n");

for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &matrix1[c][d]);

printf("Enter number of rows and columns of matrix2 \n");
scanf("%d%d", &p, &q);

if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of matrix2 \n");

for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &matrix2[c][d]);

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + matrix1[c][k]*matrix2[k][d];
}

multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of the matrices:\n");

for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

Output :

C program for matrix Multiplication

Write A Comment