Program 42 - CSU1128 - CSE 2026 - Shoolini University

Program to multiply two matrices entered by the user

Explanation

The program multiplies two matrices entered by the user and prints the result matrix. It ensures that the matrices are compatible for multiplication (i.e., the number of columns of the first matrix must equal the number of rows of the second matrix).

Header

The code begins with including the standard input-output header file using #include <stdio.h>.

Variable Declaration

The main() function is defined, and several variables are declared:

  • int d1, m1, d2, m2: Stores the number of rows and columns for the two matrices.
  • int A[d1][m1], B[d2][m2], C[d1][m2]: Declares three 2D arrays to store the elements of the two input matrices and the result matrix.

Program Description

The program prints a description message for the user to understand its functionality.

User Input

The user is prompted to enter the sizes and elements of the two matrices:

  • printf("Enter the size (number of rows and columns) of both matrices (row1 col1 row2 col2): ");: Prompts the user to enter the number of rows and columns for both matrices.
  • scanf("%d %d %d %d", &d1, &m1, &d2, &m2);: Reads the sizes and stores them in d1, m1, d2, and m2.

Compatibility Check

The program checks if the matrices are compatible for multiplication:

  • if (m1 != d2): Checks if the number of columns of the first matrix equals the number of rows of the second matrix.
  • printf("Error: Matrices cannot be multiplied.\n");: Prints an error message if the matrices are not compatible.
  • return 1;: Exits the program if the matrices cannot be multiplied.

Matrix Input

The program reads the elements of the two matrices from the user:

  • printf("Enter the elements of matrix A row-wise:\n");: Prompts the user to enter the elements of the first matrix.
  • for (int o = 0; o < d1; o++): Outer loop to iterate over each row of the first matrix.
  • for (int n = 0; n < m1; n++): Inner loop to iterate over each column of the first matrix.
  • scanf("%d", &A[o][n]);: Reads each element and stores it in the array A.
  • printf("Enter the elements of matrix B row-wise:\n");: Prompts the user to enter the elements of the second matrix.
  • for (int o = 0; o < d2; o++): Outer loop to iterate over each row of the second matrix.
  • for (int n = 0; n < m2; n++): Inner loop to iterate over each column of the second matrix.
  • scanf("%d", &B[o][n]);: Reads each element and stores it in the array B.

Matrix Multiplication

The program multiplies the two matrices and stores the result in the third matrix:

  • for (int o = 0; o < d1; o++): Outer loop to iterate over each row of the result matrix.
  • for (int n = 0; n < m2; n++): Inner loop to iterate over each column of the result matrix.
  • C[o][n] = 0;: Initializes each element of the result matrix to 0.
  • for (int j = 0; j < m1; j++): Inner loop to perform the multiplication and addition.
  • C[o][n] += A[o][j] * B[j][n];: Multiplies corresponding elements and adds the result to the current element of the result matrix.

Output Result

The program prints the result matrix:

  • printf("Result of matrix multiplication:\n");: Prints a label for the result matrix.
  • for (int o = 0; o < d1; o++): Outer loop to iterate over each row of the result matrix.
  • for (int n = 0; n < m2; n++): Inner loop to iterate over each column of the result matrix.
  • printf("%d ", C[o][n]);: Prints each element of the result matrix followed by a space.
  • printf("\n");: Prints a newline after each row.

Program End

The program ends with return 0; to indicate successful execution.

Code

                    
/*
 * -----------------------------------------------------------
 * Logic Building with Computer Programming (CSU1128)
 * Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
 * 
 * This code is a part of the educational initiative by dmj.one
 * with aim of empowering and inspiring learners in the field of
 * Computer Science and Engineering through the respective courses.
 * 
 * (c) 2022, Divya Mohan for dmj.one. All rights reserved.
 * -----------------------------------------------------------
 */

#include <stdio.h>
int main()
{
    printf("\n\n Program to multiply two matrices entered by the user. \n\n");

    // Declare variables
    int d1, m1, d2, m2;
    printf("Enter the size (number of rows and columns) of both matrices (row1 col1 row2 col2): ");
    scanf("%d %d %d %d", &d1, &m1, &d2, &m2);

    // Check if the matrices are compatible for multiplication
    if (m1 != d2)
    {
        printf("Error: Matrices cannot be multiplied.\n");
        return 1;
    }

    // Declare and initialize the matrices
    int A[d1][m1], B[d2][m2], C[d1][m2];
    printf("Enter the elements of matrix A row-wise:\n");
    for (int o = 0; o < d1; o++)
    {
        for (int n = 0; n < m1; n++)
        {
            scanf("%d", &A[o][n]);
        }
    }
    printf("Enter the elements of matrix B row-wise:\n");
    for (int o = 0; o < d2; o++)
    {
        for (int n = 0; n < m2; n++)
        {
            scanf("%d", &B[o][n]);
        }
    }

    // Perform matrix multiplication
    for (int o = 0; o < d1; o++)
    {
        for (int n = 0; n < m2; n++)
        {
            C[o][n] = 0;
            for (int j = 0; j < m1; j++)
            {
                C[o][n] += A[o][j] * B[j][n];
            }
        }
    }

    // Print the result
    printf("Result of matrix multiplication:\n");
    for (int o = 0; o < d1; o++)
    {
        for (int n = 0; n < m2; n++)
        {
            printf("%d ", C[o][n]);
        }
        printf("\n");
    }

    return 0;
}

                

Output

Program to multiply two matrices entered by the user.

Enter the size (number of rows and columns) of both matrices (row1 col1 row2 col2): 2 2 2 2
Enter the elements of matrix A row-wise: 2 4 3 5
Enter the elements of matrix B row-wise: 2 6 7 3
Result of matrix multiplication:
32 24
41 33