Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Matrix Multiplication

Updated 20 Oct 2026

Matrix multiplication combines two two-dimensional arrays into a third one. This program reads the sizes of two matrices, checks that they can be multiplied, reads the elements, finds the product and prints it. Everything is written inside main.

Definition

Two matrices $A$ and $B$ can be multiplied only when the number of columns of $A$ is equal to the number of rows of $B$. If $A$ has $m$ rows and $n$ columns, and $B$ has $p$ rows and $q$ columns, the product exists only when $n = p$. The product $C$ then has $m$ rows and $q$ columns.

Each element of $C$ is found by multiplying the elements of one row of $A$ with the matching elements of one column of $B$ and adding them:

C[i][j] = A[i][0] * B[0][j] + A[i][1] * B[1][j] + ... + A[i][n-1] * B[n-1][j]

For example, if $A$ is $2 \times 3$ and $B$ is $3 \times 2$, then $C$ is $2 \times 2$. If $A$ is $2 \times 3$ and $B$ is $2 \times 2$, the columns of $A$ (3) are not equal to the rows of $B$ (2), so multiplication is not possible.

Algorithm

  1. Create a Scanner object and read the rows m and columns n of matrix A.
  2. Read the rows p and columns q of matrix B.
  3. If any of m, n, p, q is zero or negative, print Invalid size and stop.
  4. Otherwise, if n is not equal to p, print Multiplication is not possible and stop.
  5. Otherwise, create the arrays a[m][n], b[p][q] and c[m][q].
  6. Read the elements of a and b using nested loops.
  7. Find the product:
    • The outer loop i runs over the rows of A, from 0 to m - 1.
    • The middle loop j runs over the columns of B, from 0 to q - 1.
    • Set c[i][j] to 0.
    • The inner loop k runs from 0 to n - 1 and adds a[i][k] * b[k][j] to c[i][j].
  8. Print the matrix c row by row.

Program

The whole program is in main: it reads the sizes, checks them, reads both matrices, multiplies them with three nested loops and prints the result.

import java.util.Scanner;
public class MatrixMultiplication
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows and columns of matrix A: ");
        int m = sc.nextInt();
        int n = sc.nextInt();
        System.out.print("Enter rows and columns of matrix B: ");
        int p = sc.nextInt();
        int q = sc.nextInt();
        if (m <= 0 || n <= 0 || p <= 0 || q <= 0)
        {
            System.out.println("Invalid size");
        }
        else if (n != p)
        {
            System.out.println("Multiplication is not possible");
        }
        else
        {
            int a[][] = new int[m][n];
            int b[][] = new int[p][q];
            int c[][] = new int[m][q];
            System.out.println("Enter the elements of matrix A:");
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    a[i][j] = sc.nextInt();
                }
            }
            System.out.println("Enter the elements of matrix B:");
            for (int i = 0; i < p; i++)
            {
                for (int j = 0; j < q; j++)
                {
                    b[i][j] = sc.nextInt();
                }
            }
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < q; j++)
                {
                    c[i][j] = 0;
                    for (int k = 0; k < n; k++)
                    {
                        c[i][j] = c[i][j] + a[i][k] * b[k][j];
                    }
                }
            }
            System.out.println("Product matrix:");
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < q; j++)
                {
                    System.out.print(c[i][j] + "\t");
                }
                System.out.println();
            }
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the sizes and the elements from the keyboard
mintNumber of rows of matrix A
nintNumber of columns of matrix A
pintNumber of rows of matrix B
qintNumber of columns of matrix B
aint[][]Stores the elements of matrix A
bint[][]Stores the elements of matrix B
cint[][]Stores the elements of the product matrix
iintLoop counter for the rows
jintLoop counter for the columns
kintLoop counter that runs along a row of A and a column of B

Sample input and output

Sample 1

Enter rows and columns of matrix A: 2 3
Enter rows and columns of matrix B: 3 2
Enter the elements of matrix A:
1 2 3
4 5 6
Enter the elements of matrix B:
7 8
9 10
11 12
Product matrix:
58	64	
139	154	

Sample 2

Enter rows and columns of matrix A: 2 3
Enter rows and columns of matrix B: 2 2
Multiplication is not possible

Sample 3

Enter rows and columns of matrix A: 0 3
Enter rows and columns of matrix B: 3 2
Invalid size

One value per line

Dry run

Take $A$ as $2 \times 3$ with rows 1 2 3 and 4 5 6, and $B$ as $3 \times 2$ with rows 7 8, 9 10 and 11 12. Here m = 2, n = 3, p = 3, q = 2, so n == p and the product is a $2 \times 2$ matrix.

ijkWorkingc[i][j]
0000 + 1 x 77
0017 + 2 x 925
00225 + 3 x 1158
0100 + 1 x 88
0118 + 2 x 1028
01228 + 3 x 1264
1000 + 4 x 728
10128 + 5 x 973
10273 + 6 x 11139
1100 + 4 x 832
11132 + 5 x 1082
11282 + 6 x 12154

The product matrix is therefore the rows 58 64 and 139 154, the same as the first sample run.

Exam tip: Check n == p before creating the arrays and reading any element, and set c[i][j] to 0 before the inner loop starts.

Variations

  1. Square matrices "Accept two square matrices of size n x n, where n is greater than 1 and less than 10, and display their product."
  2. Transpose and multiply "Find the transpose of a matrix and multiply it with the original matrix."
  3. Diagonal sum "Display the product matrix and the sum of its diagonal elements."