Java Program to Subtract Two Matrices

In the previous article, we have discussed Java Program to Add Two Matrices

In this article we are going to see how we can write a program to calculate the difference of two matrices in JAVA language.

Java Program to Subtract Two Matrices

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Let’s see different ways to subtract two matices

Method-1: Java Program to Subtract Two Matrices By Static Initialization of Array Elements

Approach:

• Initialize and declare two arrays of size 3×3 with elements.
• Use two for loops to iterate the rows and columns and then subtract the elements present at the same index of both matrices, then store them in another array of same size.
• Print the resultant array.

Program:

import java.util.Scanner;
public class Matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int mat1[][] = {{19,25,32},{40,54,62},{70,20,60}};
int mat2[][] =  {{50,15,10},{98,45,78},{23,73,50}};
int res[][] = new int[3][3];
int row, col ;
// Printing the matrices using our user-defined function
System.out.print("Matrix 1 : ");
printMatrix(mat1);
System.out.print("\nMatrix 2 : ");
printMatrix(mat2);

// Loops to subtract both the matrix elements and store them
for(row=0;row<3;row++)
for(col=0;col<3;col++)
res[row][col]=mat1[row][col]-mat2[row][col];

System.out.print("\nAfter subtraction of both the matrix : ");
printMatrix(res);
}

// method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
}


Output:

Matrix 1 :
19 25 32
40 54 62
70 20 60
Matrix 2 :
50 15 10
98 45 78
23 73 50
After subtraction of both the matrix :
-31 10 22
-58 9 -16
47 -53 10

Method-2: Java Program to Subtract Two Matrices By Dynamic Initialization of Array Elements

import java.util.Scanner;
public class Matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int mat1[][] = new int[3][3];
int mat2[][] =  new int[3][3];
int res[][] = new int[3][3];
int row, col ;

// Taking matrix1 input
System.out.println("\nEnter the first matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
mat1[row][col] = scan.nextInt();

System.out.print("Matrix 1 : ");
//calling printMatrix() method to print matrix-1
printMatrix(mat1);

// Taking matrix2 input
System.out.println("\nEnter the second matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
mat2[row][col] = scan.nextInt();

System.out.print("Matrix 2 : ");
//calling printMatrix() method to print matrix-2
printMatrix(mat2);

// Loops to subtract two matrices and store them
for(row=0;row<3;row++)
for(col=0;col<3;col++)
res[row][col]=mat1[row][col]-mat2[row][col];

System.out.print("\nAfter sub");
printMatrix(res);
}

// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
}


Output:

Enter the first matrix elements : 1 2 3 4 5 6 7 8 9
Matrix 1 :
1 2 3
4 5 6
7 8 9
Enter the second matrix elements : 1 2 3 4 5 6 7 8 9
Matrix 2 :
1 2 3
4 5 6
7 8 9
After sub
0 0 0
0 0 0
0 0 0

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: