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

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

  1. Read the number of rows m and columns n once, because both matrices have the same order. If either is not from 1 to 10, print Invalid size and stop.
  2. Create the arrays a and b of order m x n and read the elements of matrix A and then of matrix B, row by row (i over the rows, j over the columns).
  3. Create the arrays s and d. For every position, store s[i][j] = a[i][j] + b[i][j] and d[i][j] = a[i][j] - b[i][j].
  4. Print the heading Sum A + B: and the matrix s, row by row with a tab after each element and a new line after each row.
  5. Print the heading Difference A - B: and the matrix d in 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
mintNumber of rows of both matrices
nintNumber of columns of both matrices
aint[][]Elements of matrix A
bint[][]Elements of matrix B
sint[][]The sum matrix
dint[][]The difference matrix
iintLoop counter over the rows
jintLoop 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 size

One value per line

Dry 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.

ija[i][j]b[i][j]s[i][j] = a + bd[i][j] = a - b
00167-5
01257-3
02347-1
104371
115273
126175

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

  1. Constructor version "Write a class with a parameterised constructor that receives the order of the matrices, and add and subtract them using separate methods."
  2. Scalar multiplication "Accept a matrix and an integer k and display the matrix multiplied by k."
  3. Different orders "Accept the orders of two matrices and display NOT POSSIBLE if they cannot be added."
  4. Diagonal sum "Display the sum matrix and the sum of its diagonal elements."