C Program to Multiply Two Matrices

Introduction

Matrix multiplication involves multiplying the rows of the first matrix by the columns of the second matrix to produce a new matrix. This guide will show you how to write a C program that multiplies two matrices.

Problem Statement

Create a C program that:

  • Takes two matrices as input from the user.
  • Multiplies the two matrices.
  • Displays the resulting matrix.

Example:

  • Input:
    • Matrix 1 (2×3):
      1 2 3
      4 5 6
      
    • Matrix 2 (3×2):
      7 8
      9 10
      11 12
      
  • Output: Resultant Matrix (2×2):
    58 64
    139 154
    

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the two input matrices, the resultant matrix, and loop counters.
  4. Input the Dimensions and Elements of the Matrices: Use loops to take input from the user for both matrices.
  5. Check Compatibility for Multiplication: Ensure the number of columns in the first matrix equals the number of rows in the second matrix.
  6. Multiply the Matrices: Use nested loops to perform matrix multiplication.
  7. Display the Resulting Matrix: Use loops to display the resulting matrix.

C Program to Multiply Two Matrices

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrices and dimensions
    int r1, c1, r2, c2;
    int matrix1[100][100], matrix2[100][100], result[100][100];
    int i, j, k;

    // Step 2: Prompt the user to enter the dimensions of the first matrix
    printf("Enter the number of rows and columns of the first matrix: ");
    scanf("%d %d", &r1, &c1);

    // Step 3: Prompt the user to enter the dimensions of the second matrix
    printf("Enter the number of rows and columns of the second matrix: ");
    scanf("%d %d", &r2, &c2);

    // Step 4: Check if matrices can be multiplied
    if (c1 != r2) {
        printf("Error: Matrices cannot be multiplied!\n");
        return 1;  // Return 1 to indicate an error
    }

    // Step 5: Input the elements of the first matrix
    printf("Enter elements of the first matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c1; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    // Step 6: Input the elements of the second matrix
    printf("Enter elements of the second matrix:\n");
    for (i = 0; i < r2; i++) {
        for (j = 0; j < c2; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Step 7: Initialize the result matrix to 0
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            result[i][j] = 0;
        }
    }

    // Step 8: Multiply the matrices
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            for (k = 0; k < c1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    // Step 9: Display the resulting matrix
    printf("Resultant matrix after multiplication:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;  // Step 10: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The variables r1, c1, r2, and c2 store the dimensions of the matrices. matrix1, matrix2, and result are 2D arrays to store the input matrices and the resulting matrix, respectively. The variables i, j, and k are used as loop counters.

Step 2: Input the Dimensions of the First Matrix

  • The program prompts the user to enter the number of rows and columns for the first matrix using scanf.

Step 3: Input the Dimensions of the Second Matrix

  • The program prompts the user to enter the number of rows and columns for the second matrix using scanf.

Step 4: Check Compatibility for Multiplication

  • The program checks if the number of columns in the first matrix (c1) equals the number of rows in the second matrix (r2). If not, matrix multiplication is not possible, and the program displays an error message and exits.

Step 5: Input the Elements of the First Matrix

  • The program uses nested for loops to take input for each element of the first matrix from the user.

Step 6: Input the Elements of the Second Matrix

  • Similarly, the program uses nested for loops to take input for each element of the second matrix.

Step 7: Initialize the Result Matrix

  • The program initializes all elements of the result matrix to 0, which will later store the product of the matrices.

Step 8: Multiply the Matrices

  • The program uses nested for loops to perform matrix multiplication:
    • Each element of the resulting matrix is calculated as the sum of the products of the corresponding elements from the rows of the first matrix and the columns of the second matrix.

Step 9: Display the Resulting Matrix

  • The program uses nested for loops to display the elements of the resulting matrix after multiplication.

Step 10: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example:

Enter the number of rows and columns of the first matrix: 2 3
Enter the number of rows and columns of the second matrix: 3 2
Enter elements of the first matrix:
1 2 3
4 5 6
Enter elements of the second matrix:
7 8
9 10
11 12
Resultant matrix after multiplication:
58 64
139 154

Another Example:

Enter the number of rows and columns of the first matrix: 2 2
Enter the number of rows and columns of the second matrix: 2 2
Enter elements of the first matrix:
1 2
3 4
Enter elements of the second matrix:
5 6
7 8
Resultant matrix after multiplication:
19 22
43 50

Conclusion

This C program demonstrates how to multiply two matrices by iterating through each element and performing the necessary multiplications and summations. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top