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
- Create a
Scannerobject and read the rowsmand columnsnof matrix A. - Read the rows
pand columnsqof matrix B. - If any of
m,n,p,qis zero or negative, printInvalid sizeand stop. - Otherwise, if
nis not equal top, printMultiplication is not possibleand stop. - Otherwise, create the arrays
a[m][n],b[p][q]andc[m][q]. - Read the elements of
aandbusing nested loops. - Find the product:
- The outer loop
iruns over the rows of A, from 0 tom - 1. - The middle loop
jruns over the columns of B, from 0 toq - 1. - Set
c[i][j]to 0. - The inner loop
kruns from 0 ton - 1and addsa[i][k] * b[k][j]toc[i][j].
- The outer loop
- Print the matrix
crow 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
| Variable | Data Type | Purpose |
|---|---|---|
| args | String[] | Command line arguments of main; not used |
| sc | Scanner | Reads the sizes and the elements from the keyboard |
| m | int | Number of rows of matrix A |
| n | int | Number of columns of matrix A |
| p | int | Number of rows of matrix B |
| q | int | Number of columns of matrix B |
| a | int[][] | Stores the elements of matrix A |
| b | int[][] | Stores the elements of matrix B |
| c | int[][] | Stores the elements of the product matrix |
| i | int | Loop counter for the rows |
| j | int | Loop counter for the columns |
| k | int | Loop 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 possibleSample 3
Enter rows and columns of matrix A: 0 3
Enter rows and columns of matrix B: 3 2
Invalid sizeDry 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.
| i | j | k | Working | c[i][j] |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 + 1 x 7 | 7 |
| 0 | 0 | 1 | 7 + 2 x 9 | 25 |
| 0 | 0 | 2 | 25 + 3 x 11 | 58 |
| 0 | 1 | 0 | 0 + 1 x 8 | 8 |
| 0 | 1 | 1 | 8 + 2 x 10 | 28 |
| 0 | 1 | 2 | 28 + 3 x 12 | 64 |
| 1 | 0 | 0 | 0 + 4 x 7 | 28 |
| 1 | 0 | 1 | 28 + 5 x 9 | 73 |
| 1 | 0 | 2 | 73 + 6 x 11 | 139 |
| 1 | 1 | 0 | 0 + 4 x 8 | 32 |
| 1 | 1 | 1 | 32 + 5 x 10 | 82 |
| 1 | 1 | 2 | 82 + 6 x 12 | 154 |
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
- 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."
- Transpose and multiply "Find the transpose of a matrix and multiply it with the original matrix."
- Diagonal sum "Display the product matrix and the sum of its diagonal elements."