# Matrix addition and subtraction in java

Let’s learn matrix addition and subtraction in java.

## Matrix addition and subtraction in java

A matrix represents a two-dimensional array. Here user enters the number of rows and columns.

Also read – for each loop in java

Nested for loop is used to read the input. Outer for loop “m” ranges from 0 to number of rows of the matrix.

Meanwhile inner for loop “n” ranges from 0 to number of columns of matrix.

The input is read using nextInt() method of class Scanner and stored in x[m][n] and y[m][n] position.

Also read – java matrix multiplication in java

After reading elements, two for loops is used to add two matrices with loop index. Then finally result is stored in z[m][n] array.

Here’s matrix addition in java and sum of matrix elements in java.

```// matrix addition 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();
}
}
// matrix java
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();
}
sc.close();
}
}

```

Output:

Similar to matrix addition in java we can write java program to subtract two matrices.

```// matrix subtraction in java
import java.util.Scanner;
public class MatrixSubtractionDemo
{
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++)
{
// subtraction java
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();
}
sc.close();
}
}

```

Output:

Matrix addition and subtraction in java: Matrix addition in java using function

1. first two matrices matrix1 and matrix2 are created.
2. these two matrices are passed as parameters to matrixAddition() method.
3. In matrixAddition() method we are checking for length and size of given array.
4. if given matrix is not of same size then matrix not of same size is printed.
5. After adding two matrices total is returned to the main method and displayed using displayMatrix() method.

Here’s the java prgoram for matrix addition in java using function or addition of two matrix in java using method.

```public class MatrixAdditionUsingFunction
{
public static void main(String[] args)
{
int[][] matrix1 = {{2, 4, 6}, {8, 2, 4}};
int[][] matrix2 = {{1, 3, 5}, {7, 9, 1}};
if(total != null)
{
displayMatrix(matrix1);
System.out.println("---------");
displayMatrix(matrix2);
System.out.println("Matrix addition in java using function: ");
displayMatrix(total);
}
}
public static void displayMatrix(int[][] matrix)
{
for(int a = 0; a < matrix.length; a++)
{
for(int b = 0; b < matrix.length; b++)
{
System.out.print(" " + matrix[a][b]);
}
System.out.println();
}
}
public static int[][] matrixAddition(int[][] x, int[][] y)
{
int arr1 = x.length;
int arr2 = x.length;
int[][] total = null;
if((arr1 != y.length) || (arr2 != y.length))
{
System.out.println("Matrices not of same size");
}
else
{
total = new int[arr1][arr2];
for(int i = 0; i < arr1; i++)
{
for(int j = 0; j < arr2; j++)
{
total[i][j] = x[i][j] + y[i][j];
}
}
}
}
}

```

Output:

2 4 6

8 2 4

1 3 5
7 9 1
Matrix addition in java using function:
3 7 11
15 11 5

Also read – java program to print 3×3 matrix

```import java.io.BufferedReader;
import java.io.IOException;
{
public static void main(String[] args) throws NumberFormatException, IOException
{
int a, b;
int[][] arr1 = new int;
int[][] arr2 = new int;
System.out.println("Please enter elements of first matrix: ");
for(a = 0; a < 2; a++)
{
for(b = 0; b < 2; b++)
{
}
}
System.out.println("Please enter elements of second matrix: ");
for(a = 0; a < 2; a++)
{
for(b = 0; b < 2; b++)
{
}
}
for(a = 0; a < 2; a++)
{
for(b = 0; b < 2; b++)
{
}
System.out.println( );
}
}
}

```

Output:

Please enter elements of first matrix:
2
4
6
8
Please enter elements of second matrix:
1
3
5
7