ISC Practicals and Theory - Matrix Addition and Subtraction
Updated 21 Oct 2026
Matrix addition and subtraction work on two two-dimensional arrays of the same order. This program, written in the main method alone, reads the order once, reads matrices A and B, and prints the sum A + B and the difference A - B, each in neat rows.
Definition
Two matrices can be added or subtracted only when they have the same number of rows and the same number of columns. If both are of order $m \times n$, the result is also of order $m \times n$. The operation is done on matching elements:
S[i][j] = A[i][j] + B[i][j]
D[i][j] = A[i][j] - B[i][j]
For example, if A is 1 2 / 3 4 and B is 5 6 / 7 8, then A + B is 6 8 / 10 12 and A - B is -4 -4 / -4 -4. Since the order of both matrices must be the same, the program reads m and n only once and uses them for both.
Algorithm
- Read the number of rows
mand columnsnonce, because both matrices have the same order. If either is not from 1 to 10, printInvalid sizeand stop. - Create the arrays
aandbof orderm x nand read the elements of matrix A and then of matrix B, row by row (iover the rows,jover the columns). - Create the arrays
sandd. For every position, stores[i][j] = a[i][j] + b[i][j]andd[i][j] = a[i][j] - b[i][j]. - Print the heading
Sum A + B:and the matrixs, row by row with a tab after each element and a new line after each row. - Print the heading
Difference A - B:and the matrixdin the same way.
Program
Everything is written inside main: the input loops, one pair of loops that fills the sum and the difference, and two pairs of loops that print them.
import java.util.Scanner;
/* Program MatrixAddSub reads two matrices of the same order
and displays their sum A + B and their difference A - B. */
public class MatrixAddSub
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); // reads the data from the keyboard
System.out.print("Enter the number of rows and columns (1 to 10): ");
int m = sc.nextInt(); // number of rows of both matrices
int n = sc.nextInt(); // number of columns of both matrices
if (m >= 1 && m <= 10 && n >= 1 && n <= 10)
{
int a[][] = new int[m][n]; // stores the elements of matrix A
int b[][] = new int[m][n]; // stores the elements of matrix B
System.out.println("Enter the elements of matrix A:");
for (int i = 0; i < m; i++) // i moves over the rows
{
for (int j = 0; j < n; j++) // j moves over the columns
{
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of matrix B:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = sc.nextInt();
}
}
int s[][] = new int[m][n]; // the sum matrix
int d[][] = new int[m][n]; // the difference matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
s[i][j] = a[i][j] + b[i][j];
d[i][j] = a[i][j] - b[i][j];
}
}
System.out.println("Sum A + B:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(s[i][j] + "\t");
}
System.out.println(); // new line after each row
}
System.out.println("Difference A - B:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(d[i][j] + "\t");
}
System.out.println();
}
}
else
{
System.out.println("Invalid size");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
m | int | Number of rows of both matrices |
n | int | Number of columns of both matrices |
a | int[][] | Elements of matrix A |
b | int[][] | Elements of matrix B |
s | int[][] | The sum matrix |
d | int[][] | The difference matrix |
i | int | Loop counter over the rows |
j | int | Loop counter over the columns |
Sample input and output
Sample 1
Enter the number of rows and columns (1 to 10): 2 3
Enter the elements of matrix A:
1 2 3
4 5 6
Enter the elements of matrix B:
6 5 4
3 2 1
Sum A + B:
7 7 7
7 7 7
Difference A - B:
-5 -3 -1
1 3 5 Sample 2
Enter the number of rows and columns (1 to 10): 1 1
Enter the elements of matrix A:
-5
Enter the elements of matrix B:
7
Sum A + B:
2
Difference A - B:
-12 Sample 3
Enter the number of rows and columns (1 to 10): 0 3
Invalid sizeDry run
Take the first sample: m = 2, n = 3, A has rows 1 2 3 and 4 5 6, B has rows 6 5 4 and 3 2 1. The order is valid because 2 and 3 are both from 1 to 10.
| i | j | a[i][j] | b[i][j] | s[i][j] = a + b | d[i][j] = a - b |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 6 | 7 | -5 |
| 0 | 1 | 2 | 5 | 7 | -3 |
| 0 | 2 | 3 | 4 | 7 | -1 |
| 1 | 0 | 4 | 3 | 7 | 1 |
| 1 | 1 | 5 | 2 | 7 | 3 |
| 1 | 2 | 6 | 1 | 7 | 5 |
The sum matrix is therefore 7 7 7 / 7 7 7 and the difference matrix is -5 -3 -1 / 1 3 5, the same as the first sample run.
Exam tip: Read the order only once, because both matrices must be of the same order, and write the loops with i for rows and j for columns every time.
Variations
- Constructor version "Write a class with a parameterised constructor that receives the order of the matrices, and add and subtract them using separate methods."
- Scalar multiplication "Accept a matrix and an integer k and display the matrix multiplied by k."
- Different orders "Accept the orders of two matrices and display NOT POSSIBLE if they cannot be added."
- Diagonal sum "Display the sum matrix and the sum of its diagonal elements."