Java Program to Add Two Matrices

In the previous article, we have discussed  Java Menu Driven Program to Perform Basic Operations on Two Matrices

In this article we are going to see how we can write a program to calculate the sum of two matrices in JAVA language.

Java Program to Add Two Matrices

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to add two matices.

Method-1: Java Program to Add Two Matrices By Static Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3 with elements.
  • Use two for loops to iterate the rows and columns and then add the elements present at the same index of both matrices, then store them in another array of same size.
  • Print the resultant array.

Program:

import java.util.Scanner;

public class Matrix
{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        //Matrix 1
        int mat1[][] = {{19,25,32},{40,54,62},{70,20,60}};
        //Matrix 2
        int mat2[][] =  {{50,15,10},{98,45,78},{23,73,50}};
        //Result Matrix
        int res[][] = new int[3][3];
        int row, col ;
        // Printing the matrices using our user-defined function
        System.out.print("Matrix 1 : ");
        printMatrix(mat1);
        System.out.print("Matrix 2 :");
        printMatrix(mat2);

    // Loops to add both the matrix elements and store them
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                res[row][col]=mat1[row][col]+mat2[row][col];
        
        System.out.print("After sum of both the matrix : ");
        printMatrix(res);
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n\n");
    }
}
Output:

Matrix 1 : 
19 25 32 
40 54 62 
70 20 60

Matrix 2 :
50 15 10 
98 45 78 
23 73 50

After sum of both the matrix : 
69 40 42 
138 99 140 
93 93 110

Method-2: Java Program to Add Two Matrices By Dynamic Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3.
  • Ask the user to input array elements for both the matrices and store them in the arrays using two for loops.
  • Use two for loops to iterate the rows and columns and then add the elements present at the same index of both matrices, then store them in another array of same size.
  • Print the resultant array.

Program:

import java.util.Scanner;

public class Matrix
{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int mat1[][] = new int[3][3];
        int mat2[][] =  new int[3][3];
        int res[][] = new int[3][3];
        int row, col ;
        
        // Taking matrix1 input
        System.out.println("Enter the first matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                mat1[row][col] = scan.nextInt();
        System.out.print("Matrix 1 : ");
        printMatrix(mat1);

        // Taking matrix2 input
        System.out.println("\nEnter the second matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                mat2[row][col] = scan.nextInt();
        System.out.print("Matrix 2 : ");
        printMatrix(mat2);

        // Loops to add both the matrix elements and store them
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                res[row][col]=mat1[row][col]+mat2[row][col];
        
        System.out.print("\nAfter sum of both the matrix : ");
        printMatrix(res);
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
    }
}

Output:

Enter the first matrix elements : 
Matrix 1 : 
1 2 3 
4 5 6 
7 8 9 
Enter the second matrix elements : 
Matrix 2 : 
1 2 3 
4 5 6 
7 8 9 
After sum of both the matrix : 
2 4 6 
8 10 12 
14 16 18

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: