# Java Program to Subtract the Two Matrices

• Last Updated : 04 Jan, 2021

Given below the two matrices A and B, the task is to subtract them, and for subtracting the matrices the size of both the matrix should be same i.e. both the matrix must have N X M dimension.

Example:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

```Input : A[][] = {{3, 1},
{2, 4}}
B[][] = {{1, 1},
{2, 1}}
Output: {{2, 0},
{0, 3}}
Input : A[][] = {{1, 2},
{3, 4}}
B[][] = {{6, 4},
{1, 3}}
Output: {{-5, -2},
{2, 1}}```

Approach:

• Initialize the two matrices of equal sizes because to perform the subtraction, the size of both the matrix should be the same.
• Assign the values in both the matrix.
• Create a new matrix of the same size as of Matrix1 and Matrix2 to store the result after subtraction.
• Traverse through the matrix with the help of for loop, and perform the subtraction on both the matrix. And store the result in the resultant matrix.
• Print the final resultant matrix.

Below is the implementation of the above approach :

## Java

 `// Java Program to Subtract the Two Matrices`` ` `import` `java.io.*;`` ` `class` `GFG {`` ` `    ``// Function to print Matrix``    ``static` `void` `printMatrix(``int` `M[][], ``int` `rowSize,``                            ``int` `colSize)``    ``{``        ``for` `(``int` `i = ``0``; i < rowSize; i++) {``            ``for` `(``int` `j = ``0``; j < colSize; j++)``                ``System.out.print(M[i][j] + ``" "``);`` ` `            ``System.out.println();``        ``}``    ``}`` ` `    ``// Function to subtract the two matrices``    ``// and store in matrix C``    ``static` `int``[][] subtract(``int` `A[][], ``int` `B[][], ``int` `size)``    ``{``        ``int` `i, j;``        ``int` `C[][] = ``new` `int``[size][size];`` ` `        ``for` `(i = ``0``; i < size; i++)``            ``for` `(j = ``0``; j < size; j++)``                ``C[i][j] = A[i][j] + B[i][j];`` ` `        ``return` `C;``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `size = ``3``;`` ` `        ``int` `A[][] = { { ``50``, ``20``, ``30` `},``                      ``{ ``60``, ``30``, ``10` `},``                      ``{ ``30``, ``80``, ``10` `} };``        ``// Print the matrices A``        ``System.out.println(``"\nMatrix A:"``);``        ``printMatrix(A, size, size);`` ` `        ``int` `B[][] = { { ``10``, ``10``, ``5` `},``                      ``{ ``20``, ``10``, ``12` `},``                      ``{ ``23``, ``21``, ``12` `} };``        ``// Print the matrices B``        ``System.out.println(``"\nMatrix B:"``);``        ``printMatrix(B, size, size);`` ` `        ``// Add the two matrices``        ``int` `C[][] = subtract(A, B, size);`` ` `        ``// Print the result``        ``System.out.println(``"\nResultant Matrix:"``);``        ``printMatrix(C, size, size);``    ``}``}`
Output
```Matrix A:
50 20 30
60 30 10
30 80 10

Matrix B:
10 10 5
20 10 12
23 21 12

Resultant Matrix:
60 30 35
80 40 22
53 101 22
```

Time Complexity: O(N x M), where N x M is the dimension of matrices.

My Personal Notes arrow_drop_up