# Matrix Addition And Subtraction In Java

Hello everyone!! Welcome to flower brackets blog. In this post we are going to learn matrix addition and subtraction in java.

How to add matrices in java?

In matrix addition java we use two-dimensional array of two matrix and we will add those matrix and finally print the values.

Also Read – Matrix Transpose Java

Here the size of the matrix will be dynamically allocated. So I am going to ask user to enter number of rows and columns.

Then we can go ahead. So let’s start adding 2d arrays java,

#### Example : matrix addition and subtraction in java

```// adding 2d arrays java

import java.util.Scanner;

{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter number of rows : ");
int row = sc.nextInt();
System.out.println("Please enter number of columns : ");
int col = sc.nextInt();

int x[][] = new int[row][col];
int y[][] = new int[row][col];

System.out.println("Please enter first matrix - ");
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
x[m][n] = sc.nextInt();
}
}

System.out.println("Please enter second matrix - ");
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
y[m][n] = sc.nextInt();
}
}

int z[][] = new int[row][col];
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
z[m][n] = x[m][n] + y[m][n];
}
}

System.out.println("The addition of two matrices is - ");
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
System.out.print(z[m][n] + " ");
}
System.out.println();
}
}
}

```

In the above example as you can see I have used two for loops to read the input from the user.

The first for loop or the outer for loop, variable “m” ranges from zero to number of rows of the matrix.

While the second for loop or the inner for loop, variable “n” ranges from zero to number of columns of the matrix.

Also Read – Matrix Multiplication In Java

Then in the inner loop the user input integers will be read using nextInt method of the scanner class.

Once two matrices are read then two for loops are used to add two matrices and the result is stored in new array and displayed like this,

Output:

#### Example : subtraction in java

On the same lines of matrix addition in java we can write a program on matrix subtraction as well.

So let’s get our hands dirty,

```import java.util.Scanner;

{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter number of rows : ");
int row = sc.nextInt();
System.out.println("Please enter number of columns : ");
int col = sc.nextInt();

int x[][] = new int[row][col];
int y[][] = new int[row][col];

System.out.println("Please enter first matrix - ");
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
x[m][n] = sc.nextInt();
}
}

System.out.println("Please enter second matrix - ");
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
y[m][n] = sc.nextInt();
}
}

int z[][] = new int[row][col];
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
z[m][n] = x[m][n] - y[m][n];
}
}

System.out.println("The subtraction of two matrices is - ");
for(int m = 0; m < row; m++)
{
for(int n = 0; n < col; n++)
{
System.out.print(z[m][n] + " ");
}
System.out.println();
}
}
}

```

Output:

##### conclusion

So that’s it. This is all about how to execute matrix addition and subtraction in java.

I hope you guys have understood the concept. You can subscribe to my blog flower brackets if haven’t already.

If you found this post useful, share it across social networks.