# Subtraction of two matrices in java

In this tutorial, we will learn how to subtract two matrices with good logic. Subtraction of two matrices in Java will be helpful in many applications of two-dimensional arrays. You may come up with these requirements in many situations.

As you all are here to learn some easy and simple code then this is the appropriate platform to refer. From this code, you will get to know many functions like scanner and two-dimensional arrays.

## Matrix subtraction in Java between Two Matrices

Let us learn this through simple examples

Firstly define the Scanner function and scan two variables one by one. Then declare three matrices of two-dimensional array type.

Scanner in = new Scanner(System.in); rows= in.nextInt(); columns = in.nextInt(); int matrix1[][] = new int[rows][columns]; int matrix2[][] = new int[rows][columns]; int diff[][] = new int[rows][columns];

To use Scanner function we need to import util.scanner package

import java.util.Scanner;

Now read each matrix using for loops

for(i=0;i<rows;i++){ for(j=0;j<column;j++){ scanf("%d",matrix1[i][j]); } }

Explanation:For each i value,read the j value from 0 to ices (column -1) .Repeat this step for matrix2 to read all the values of matrix.

To find the difference of both matrices, both matrices should have equal number of rows and equal number of columns. The difference matrix generated will also have same number of rows and columns.So the difference is calculated by subtracting corresponding elements of each matrix.

for(i=0;i<rows;i++){ for(j=0;j<columns;j++){ diff[i][j]=matrix1[i][j]-matrix2[i][j] } }

Now print this diff matrix, which is the output for our given problem.

Code for subtractions of matrices is:

### Java code to subtract two matrices

import java.util.Scanner; class MatrixSubtraction{ public static void main(String args[]){ int rows,columns, i, j; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows and columns of matrix"); rows= in.nextInt(); columns = in.nextInt(); int matrix1[][] = new int[rows][columns]; int matrix2[][] = new int[rows][columns]; int diff[][] = new int[rows][columns]; System.out.println("Enter the elements of matrix1"); for ( i = 0 ; i < rows ; i++ ){ for ( j = 0 ; j < columns ; j++ ){ matrix1[i][j] = in.nextInt(); } } System.out.println("Enter the elements of matrix2"); for ( i = 0 ; i < rows ; i++ ){ for ( j = 0 ; j < columns ; j++ ){ matrix2[i][j] = in.nextInt(); } } for ( i = 0 ; i < rows ; i++ ){ for ( j = 0 ; j < columns ; j++ ){ diff[c][d] = matrix1[i][j] - matrix2[i][j]; } } System.out.println("Difference of entered matrices:-"); for ( i = 0 ; i < rows ; i++ ){ for ( j = 0 ; j < columns ; j++ ){ System.out.print(diff[i][j]+"\t"); } System.out.println(); } } }

Output:

```
Output is:
Enter the number of rows and columns of matrix
2
2
Enter the elements of matrix1
8
6
8
5
7
3
Enter the elements of matrix2
9
5
2
4
7
6
Difference of entered matrices:-
-1 1
6 1
```

Learn:

## Leave a Reply