Matrices in java – Java Program to Check Whether Two Matrices are Equal or Not

Matrices in java: In the previous article, we have discussed Java Program to Multiply Two Matrices

In this article we are going to see how we can write a program to check if two matrices are equal in JAVA language.

Java Program to Check Whether Two Matrices are Equal or Not

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 check if two matrices are equal or not.

Method-1: Java Program to Check Whether Two Matrices are Equal or Not 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, then compare the elements at same indexes. If any element is different break out of the loop.
  • If all the elements are same, then both these are equal, else not .

Program:

public class Matrix
{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int mat1[][] = {{19,25,32},{40,54,62},{70,20,60}};
        int mat2[][] =  {{50,15,10},{98,45,78},{23,73,50}};
        int row, col ;
        boolean flag = true;
        // Printing the matrices using our user-defined function
        System.out.print("\nMatrix 1");
        printMatrix(mat1);
        System.out.print("\nMatrix 2");
        printMatrix(mat2);

        // Loops to check if two matrices are equal
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                // Checks whether each element is same in both matrices
                if(mat1[row][col]!=mat2[row][col])
                {
                    // Sets flag to false and breaks out of the loop if any element is not same
                    flag = false;
                    break;
                }
            }
        if(flag)
            System.out.println("\n\nBoth the matrices are equal");
        else
            System.out.println("\n\nBoth the matrices are not equal");
    }

    // method 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");
    }
}


Output:

Matrix 1
19 25 32 
40 54 62 
70 20 60

Matrix 2
50 15 10 
98 45 78 
23 73 50


Both the matrices are not equal

Method-2: Java Program to Check Whether Two Matrices are Equal or Not By Dynamic Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3.
  • Ask the user for input and store them in the arrays using two for loops.
  • Use two for loops to iterate the rows and columns, then compare the elements at same indexes. If any element is different break out of the loop.
  • If all the elements are same, then both these are equal, else not .

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];
        boolean flag = true;
        int row, col ;
        
        // Taking matrix1 input
        System.out.println("\nEnter 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 : "); 
        //calling printMatrix() method to 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 : "); 
        //calling printMatrix() method to print matrix-1 
        printMatrix(mat2);

        // Loops to check if two matrices are equal
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                // Checks whether each element is same in both matrices
                if(mat1[row][col]!=mat2[row][col])
                {
                    // Sets flag to false and breaks out of the loop if any element is not same
                    flag = false;
                    break;
                }
            }
        if(flag)
            System.out.println("\n\nBoth the matrices are equal");
        else
            System.out.println("\n\nBoth the matrices are not equal");
    }

    // Method 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");
    }
}

Output:

CASE-1(Equal)

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

Both the matrices are equal

CASE-2(Not equal)

Enter the first matrix elements
Matrix 1 : 
1 2 3 
4 5 6 
7 8 9

Enter the second matrix elements
Matrix 2 : 
9 8 7 
6 5 4 
3 2 1

Both the matrices are not equal

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: