C Program for Matrix Multiplication

Here is the C program for matrix multiplication. We have to take two matrix as input form user and multiply them to get product of two input matrices. We can multiply two matrices only if the number of columns of the 1st matrix must equal the number of rows of the 2nd matrix.


Points to remember

  • To multiply an m×n matrix by an n×p matrix, the n must be the same, and the result is an m×p matrix.
  • The time complexity of this c program is O(n3).
For Example: If we multiply a 2×4 matrix and a 4×3 matrix, then the product matrix will be a 2×3 matrix.

C program for matrix multiplication

This program first takes the order of first matrix as input from user and then populates the elements of first matrix by taking input from user. Then it asks user to enter the order of second matrix. It then checks whether column count of first matrix is equal to row count of second matrix. If not then it prints "Matrices cannot be multiplied". To multiply both matrices, it uses the algorithm mentioned here matrix multiplication Algorithm.

#include <stdio.h>

int main(){
    int rows1, cols1, rows2, cols2, rowCounter, colCounter, i, j, k;
    int firstMatrix[50][50], secondMatrix[50][50];
    int productMatrix[50][50] = {0};
    
    printf("Enter Rows and Columns of first matrix\n");
    scanf("%d %d", &rows1, &cols1);
    
    /* Input first matrix */
    printf("Enter first Matrix of size %dX%d\n", rows1, cols1);
    for(rowCounter = 0; rowCounter < rows1; rowCounter++){
        for(colCounter = 0; colCounter < cols1; colCounter++){
            scanf("%d", &firstMatrix[rowCounter][colCounter]);
        }
    }

    /* Input second matrix */
    printf("Enter Rows and Columns of second matrix\n");
    scanf("%d %d", &rows2, &cols2);
    
    if(cols1 != rows2){
        printf("Matrices cannot be multiplied\n");
    } else {
        printf("Enter second Matrix of size %dX%d\n", cols2, rows2);
        for(rowCounter = 0; rowCounter < rows2; rowCounter++){
            for(colCounter = 0; colCounter < cols2; colCounter++){
                scanf("%d", &secondMatrix[rowCounter][colCounter]);
            }
        }

        /* Multiply both matrices */ 
        for(i = 0; i < rows1; i++){
            for(j = 0; j < rows2; j++){
                for(k = 0; k < cols2; k++){
                 productMatrix[i][j] += firstMatrix[i][k]*secondMatrix[k][j];
                } 
            }
        }

        /* Print product matrix */
        printf("Product Matrix\n");
        for(rowCounter = 0; rowCounter < rows1; rowCounter++){
            for(colCounter = 0; colCounter < cols2; colCounter++){
                printf("%d ", productMatrix[rowCounter][colCounter]);
            }
            printf("\n");
        }
    }
    
    return 0;
}
Output
Enter Rows and Columns of first Matrix
2 3
Enter first Matrix of size 2X3
1 2 3
4 5 6
Enter Rows and Columns of second matrix
3 1
Enter second Matrix of size 3X1
7
8
9
Product Matrix
7
28
Enter Rows and Columns of first Matrix
3 3
Enter first Matrix of size 3X3
1 2 3
4 5 6
7 8 9
Enter Rows and Columns of second matrix
3 3
Enter second Matrix of size 3X3
1 0 1
0 1 1
1 1 1
Product Matrix
4 5 6
10 11 15
16 17 24
Enter Rows and Columns of first Matrix
2 2
Enter first Matrix of size 3X3
1 2
3 4 
Enter Rows and Columns of second matrix
1 2
Matrices cannot be multiplied

Related Topics
C program to add two matrix
C Program for subtraction of two matrix
C program to print lower triangular matrix
C program to find scalar multiplication of a matrix
C Program to find transpose of matrix
C program to check identity matrix
C Program to print fibonacci series
C program to check a number is palindrome or not
List of all C programs