C Program to Multiply Two Matrices

Howdy readers, today you will learn how to multiply two matrices using C Programming language.

Multiplication of two matrices A and B is only possible to exist when the number of columns in the first matrix is equal to the number of rows in the second matrix.

Now, let’s understand how to multiply two matrices:

  1. Make sure that the no. of columns in the 1st matrix equals the no. of rows of the second matrix.
  2. Multiply the elements of each row of the 1st matrix by the elements of each column in the 2nd matrix.
  3. Add the products.
  4. Place the added products in the respective order.

We will be using the above steps in our program to multiply two matrices.

So, without any delay, let’s begin this tutorial.

C Program to Multiply Two Matrices

C Program

// C Program to Multiply Two Matrices
#include <stdio.h>
int main(){
    int r1, r2, c1, c2, k, i, j, sum = 0;
    int a[10][10], b[10][10], multiply[10][10];
    
    // Asking for input
    printf("Enter the no. of rows and columns of first matrix: \n");
    scanf("%d %d", &r1, &r2 );
    printf("Enter the elements of first matrix: \n");
    for (i = 0; i < r1; i++){
        for (j = 0; j < c1; j++){
            scanf("%d", &a[i][j]);
        }
    }
    
    printf("Enter the no. of rows and columns of second matrix: \n");
    scanf("%d %d", &r2, &c2);
    printf("Enter the elements of second matrix: \n");
    for (i = 0; i < r2; i++){
        for (j = 0; j < c2; j++){
            scanf("%d", &b[i][j]);
        }
    }
    
    if (c1 != r2)
        printf("Multiplication is not possible.");
    else{
        for (i = 0; i < r1; i++){
            for (j = 0; j < c2; j++){
                for (k = 0; k < r2; k++){
                    sum = sum + a[i][k] * b[k][j];
                }
                multiply[i][j] = sum;
                sum = 0;
            }
        }
        
        // Displaying output
        printf("Product of two matrices: ");
        for (i = 0; i < r1; i++){
            for (j = 0; j < c2; j++){
                printf("%d ", multiply[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

Output 1 

Enter the no. of rows and columns of first matrix: 
2 3
Enter the elements of first matrix: 
1 2 3
4 5 6

Enter the no. of rows and columns of second matrix: 
3 2
Enter the elements of second matrix: 
2 4
4 6
3 1

Product of two matrices: 
19 19 
46 52 

Output 2

Enter the no. of rows and columns of first matrix: 
2 2
Enter the elements of first matrix: 
3 2
4 5

Enter the no. of rows and columns of second matrix: 
3 2
Enter the elements of second matrix: 
4 5
4 6  
5 7

Multiplication is not possible.   
C Program to Multiply Two Matrices

Explanation

    int r1, r2, c1, c2, k, i, j, sum = 0;
    int a[10][10], b[10][10], multiply[10][10];

In this program, we have declared 8 int data type variables and three arrays.

    // Asking for input
    printf("Enter the no. of rows and columns of first matrix: \n");
    scanf("%d %d", &r1, &r2 );
    printf("Enter the elements of first matrix: \n");
    for (i = 0; i < r1; i++){
        for (j = 0; j < c1; j++){
            scanf("%d", &a[i][j]);
        }
    }
    
    printf("Enter the no. of rows and columns of second matrix: \n");
    scanf("%d %d", &r2, &c2);
    printf("Enter the elements of second matrix: \n");
    for (i = 0; i < r2; i++){
        for (j = 0; j < c2; j++){
            scanf("%d", &b[i][j]);
        }
    }

Now, the user is asked to enter the order and elements of each of the matrices.

    if (c1 != r2)
        printf("Multiplication is not possible.");

We check whether the no. of columns of the first matrix is equal to no. of rows of the second matrix or not.

    else{
        for (i = 0; i < r1; i++){
            for (j = 0; j < c2; j++){
                for (k = 0; k < r2; k++){
                    sum = sum + a[i][k] * b[k][j];
                }
                multiply[i][j] = sum;
                sum = 0;
            }
        }

Then, we multiply the elements of each row of the 1st matrix by the elements of each column of the 2nd matrix.

Then, we add the products. This gives us the product of two matrices.

        // Displaying output
        printf("Product of two matrices: ");
        for (i = 0; i < r1; i++){
            for (j = 0; j < c2; j++){
                printf("%d ", multiply[i][j]);
            }
            printf("\n");
        }
    }

Finally, the product of two matrices is displayed on the screen in correct order using printf() function and for loop statement.

Conclusion

I hope after going through this post, you understood how to multiply two matrices using C Programming language.

If you face any problem while understanding this program, then let us know in the comment section. We will be delighted to solve your query.

Also Read:

Leave a Comment