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 using nextInt() method of Scanner class.

Nested for loop is used to store the input entered by user in given two dimensional integer array. 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 Scanner class and stored in x[m][n] and y[m][n] two dimensional array.

After reading elements, two for loops is used to add two matrices elements with loop index. Then finally result is stored in z[m][n] two dimensional array. Here’s matrix addition in java.

import java.util.Scanner;
public class MatrixAdditionDemo
{
   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++)
         {
            // matrix addition in java
            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:

matrix addition and subtraction in java

Similar to above java program we can write 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

Also read – variables in java