You are here

Matrix multiplication in C

Matrix multiplication in C language: C program to multiply two matrices (two-dimensional array) which will be entered by the user. A user will enter the order of a matrix and then its elements and similarly input second matrix. If the entered orders of two matrices are such that they can't be multiplied with each other then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics.

Matrix multiplication in C language

#include <stdio.h>
 
int main()
{
  int m, n, p, q, c, d, 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 elements of first matrix\n");
 
  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      scanf("%d", &first[c][d]);
 
  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 second matrix\n");
 
    for (c = 0; c < p; c++)
      for (d = 0; d < q; d++)
        scanf("%d", &second[c][d]);
 
    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }
 
        multiply[c][d] = sum;
        sum = 0;
      }
    }
 
    printf("Product of entered matrices:-\n");
 
    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++)
        printf("%d\t", multiply[c][d]);
 
      printf("\n");
    }
  }
 
  return 0;
}

Download Matrix multiplication program.

An output of 3 X 3 matrix multiplication C program is shown as an example below.

Matrix multiplication program

Matrices are frequently used in programming and are used to represent graph data structure, in solving a system of linear equations and in many other ways. A lot of research is being done on how to multiply matrices using a minimum number of operations. You can also implement this program using pointers.