Java Program to Add Two Matrices

In this post, we will learn how to add two matrices using multi-dimensional arrays in Java Programming language. Let’s understand about Matrix and Matrix Addition before jumping into the Java Program to Add Two Matrices.

Java Program to Add Two Matrices

A Matrix is a rectangular array of numbers that is arranged in the form of rows and columns. For example: A Matrix of order 3 x 3 look like this:

Example of a 3x3 matrix

To Add Matrices, there are certain rules:

  1. Matrices should be of same dimension (i.e. – Same no. of rows and columns)
  2. Matrix Addition is commutative (i.e. A + B = B + A)
  3. Matrix Addition is Associative (i.e. (A + B) + C = A + (B + C))

Matrix addition can only be performed if both the matrices are of same order. In Matrix addition, the element at position ith row and jth column of Matrix A is added with the element at position ith row and jth column of Matrix B.

i.e. result[i][j] = matA[i][j] + matB[i][j]

java matrix addition

Now, let’s get straight into the Java Program to Add Two Matrices.

Java Program to Add Two Matrices

import java.util.*;

public class Main{
    

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the number of rows of Matrices: ");
        int row = sc.nextInt();
        
        System.out.println("Enter the number of columns of Matrices: ");
        int col = sc.nextInt();
        
        int[][] matA = new int[row][col];
        int[][] matB = new int[row][col];
        
        System.out.println("Enter the values of Matrix A:");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                matA[i][j] = sc.nextInt();
            }
        }
        
        System.out.println("Enter the values of Matrix B:");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                matB[i][j] = sc.nextInt();
            }
        }
        
        // Adding Matrix A and Matrix B
        
        int[][] resultantMatrix = new int[row][col];
        
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                resultantMatrix[i][j] = matA[i][j] + matB[i][j];
            }
        }
        
        //Displaying the resultant matrix
        System.out.println("Sum of Matrix A and Matrix B is ");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                System.out.print(resultantMatrix[i][j]+" ");
            }
            System.out.println();
        }
        
    }
    
    
}

Output

Enter the number of rows of Matrices: 3

Enter the number of columns of Matrices: 3

Enter the values of Matrix A:


1 2 3
1 2 3
1 2 3
Enter the values of Matrix B:


1 1 1
2 2 2
1 2 3

Sum of Matrix A and Matrix B is

2 3 4 
3 4 5 
2 4 6 

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        
        System.out.println("Enter the number of rows of Matrices: ");
        int row = sc.nextInt();
        
        System.out.println("Enter the number of columns of Matrices: ");
        int col = sc.nextInt();

In this program, we first take the input of number of rows and columns of the matices by using the Scanner class in Java.

        int[][] matA = new int[row][col];
        int[][] matB = new int[row][col];
        
        System.out.println("Enter the values of Matrix A:");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                matA[i][j] = sc.nextInt();
            }
        }
        
        System.out.println("Enter the values of Matrix B:");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                matB[i][j] = sc.nextInt();
            }
        } 

Then, we declare two matrices and take the elements of both the matrices as input from the user.

       // Adding Matrix A and Matrix B
        
        int[][] resultantMatrix = new int[row][col];
        
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                resultantMatrix[i][j] = matA[i][j] + matB[i][j];
            }
        }

Now, we add each element of the Matrix A with that of the Matrix B and stores it result as the resultant matrix element. i.e. resultantMatrix[i][j] = matA[i][j] + matB[i][j]

when i=0 , j=0 then resultantMatrix[0][0] = matA[0][0] + matB[0][0] = 1 + 1 = 2
then, i = 0 but j = 1, resultantMatrix[0][1] = matA[0][1] + matB[0][1] = 2 + 1 = 3
then, i = 0 , j = 2, resultantMatrix[0][2] = matA[0][2] + matB[0][2] = 3 + 1 = 4
Addition of first row is completed.

After the inner for loop completes then i is incremented to i = 1, and again inner for loops starts from j = 0 to j = 2
then, i = 1, j = 0 and resultantMatrix[1][0] = matA[1][0] + matB[1][0] = 1 + 2 = 3
then, i = 1, j = 1 and resultantMatrix[1][1] = matA[1][1] + matB[1][1] = 2 + 2 = 4
then, i = 1, j = 2 and resultantMatrix[1][2] = matA[1][2] + matB[1][2] = 3 + 2 = 5
Hence, Addition of second row also completed.

Now, for the third row,
After the inner for loop completes then i then increments to i = 2, and again inner for loops starts from j = 0 to j = 2
then, i = 2, j = 0 and resultantMatrix[2][0] = matA[2][0] + matB[2][0] = 1 + 1 = 2
then, i = 2, j = 1 and resultantMatrix[2][1] = matA[2][1] + matB[2][1] = 2 + 2 = 4
then, i = 2, j = 2 and resultantMatrix[2][2] = matA[2][2] + matB[2][2] = 3 + 3 = 6
Therefore, Addition of third row also completed. Hence, Addition of Matrix A and Matrix B is calculated and stored in the resultant matrix.

//Displaying the resultant matrix
        System.out.println("Sum of Matrix A and Matrix B is ");
        for(int i=0;i<row;i++){
            for(int j=0; j<col ; j++){
                System.out.print(resultantMatrix[i][j]+" ");
            }
            System.out.println();
        }

Then we finally display the Resultant Matrix. This is the Java Program to Add Two Matrices Using Multi-Dimensional Arrays.

Conclusion

I hope after going through this post, you understand how to code Java Program to Add Two Matrices Using Multi-Dimensional Arrays.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *