Summary: In this programming example, we will learn how to multiply two matrices in C using nested for loops.

A matrix in C programming language can be represented using a 2D array, where the order of the matrix is the dimensions of the 2D array.

Since matrices can be represented in C, they can be also be multiplied easily using the C programming language.

Condition for Matrix Multiplication

Two matrices can only be multiplied if the number of columns of the first matrix is equal to the number of rows of the second matrix.

Matrix multiplication condition

If the multiplication is valid, the order of the resultant matrix would be (row of the first matrix * column of the second matrix).

Therefore, in order to multiply two matrices, we first have to verify their order.

Matrix Multiplication

To multiply two matrices:

  • Declare a 2D array to hold resultant matrix values.
  • Loop (I) from 0 to row order of the first matrix.
    • Nest (J) another loop from 0 to the column order of the second matrix.
      • Nest another loop (K) from 0 to row order of the second matrix.
        • Sum (matrix1[I][K] * matrix2[K][J])
      • Store the final sum into the resultant matrix as res[I][J] = sum.
    • End (J) loop.
  • End (I) loop.

Here is an example of multiplication of two matrices in C:

#include <stdio.h>
#include <stdlib.h>
#define MAX 10
 
//method to input elements in the array
void inputElement(int m[MAX][MAX],int r, int c){
    for(int i=0; i<r ; i++){
        for(int j=0; j<c; j++){
            printf("Enter element into row %d & column %d \n",i+1,j+1);
            scanf("%d",&m[i][j]);
        }
    }
}
 
//maethod to display matrix
void display(int m[MAX][MAX],int r, int c){
    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            printf("%d \t",m[i][j]);
        }
        printf("\n");
    }
}
 
int main()
{
    //order of the two matrix
    int r1, r2, c1, c2;
    
    printf("Enter order for Matrix I as: rows columns\n");
    scanf("%d", &r1);
    scanf("%d", &c1);
 
    printf("Enter order for Matrix II as: rows columns\n");
    scanf("%d", &r2);
    scanf("%d", &c2);
 
    //Checking whether multiplication is possible or not
    //If not possible then stop execution by return
    if(c1 != r2){
        printf("Multiplication not possible with available matrix order \n");
        return 0;
    }
    
    //If order validates multiplication then proceed
    //Declare matrices
    int matrix1[MAX][MAX];
    int matrix2[MAX][MAX];
 
    //Take input in the both the matrices
    printf("Enter %d elements into Matrix I \n",r1 * c1);
    inputElement(matrix1, r1, c1);
    printf("Enter %d elements into Matrix II \n",r2 * c2);
    inputElement(matrix2, r2, c2);
 
    //Declare resultant matrix
    int res[MAX][MAX] ; 
    
    //Multiplying matrices
    for(int i=0; i<r1 ; i++){
        for(int j=0; j<c2; j++){
            int sum =0;
            for(int k=0; k<r2; k++){
                sum += (matrix1[i][k] * matrix2[k][j]);
            }
            res[i][j] = sum;
        }
    }
 
    printf("--------Matrix I------------ \n");
    display(matrix1, r1, c1);
 
    printf("--------Matrix II------------ \n");
    display(matrix2, r2, c2);
 
    printf("--------Resultant Matrix------------\n");
    display(res, r1, c2);
    
    return 0;
}

Output:

Matrix multiplication in C

In this tutorial, we learned to multiply two matrices in C. If you have any suggestiong or doubts then comment below.

Leave a Reply

two + seventeen =