Java Program to Check The Equality of Two Matrices

Check The Equality of Two Matrices

Java program to check the equality of two matrices has been shown here. Two matrices $[A]_{m \times n}$ and $[B]_{p \times q}$ are considered to be equal if both of the following conditions are satisfied

(i) Number of rows and columns are same for both of the matrices i.e. $m = p$ and $n = q$

(ii) Each elements of $A$ is equal to corresponding element of $B$ i.e. $A_{ij} = B_{ij}$ for each $i \in m$ and $j \in n$.






1. Java Program to check the equality of two matrices

Code has been copied
/**********************************************
            Alphabetacoder.com
Java program to check equality of two matrices
***********************************************/

import java.util.Scanner;
public class MatrixEquality{
    public static void main(String args[]){
        //System.in is a standard input stream
        // sc is the object
        Scanner sc= new Scanner(System.in);
        int m,n,p,q,flag=0,i,j;
        
        //take input of the order of first matrix
        System.out.print("Enter the number of row and column of first matrix=");
        m=sc.nextInt();
        n=sc.nextInt();
        
        //declare first matrix
        int A[][]=new int[m][n];
        //take input of the first matrix
        System.out.print("Enter the first matrix of order "+m+" x "+n+"=\n");
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                A[i][j]=sc.nextInt();
            }
        }
         //take input of the order of second matrix
        System.out.print("Enter the number of row and column of second matrix=");
        p=sc.nextInt();
        q=sc.nextInt();
        
        //declare first matrix
        int B[][]=new int[p][q];
        //take input of the first matrix
        System.out.print("Enter the second matrix of order "+p+" x "+q+"=\n");
        for(i=0;i<p;i++){
            for(j=0;j<q;j++){
                B[i][j]=sc.nextInt();
            }
        }
        
        // check if order of matrices are same
        // if not same order then check each corresponding elements
        if(m!=p||n!=q){
            System.out.print("\nMatrices are of different order,hence not equal");
            flag=1;
        }
        else{
            //check equality of each corresponding elements
            for(i=0;i<m;i++){
                for(j=0;j<n;j++){
                    if(A[i][j]!=B[i][j]){
                        // inequality spotted 
                        System.out.print("\nMatrices are not equal. Element mismatch at "+(i+1)+" row "+(j+1)+" column");
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                    break;
            }
        }
        if(flag==0)
            System.out.print("\nMatrices are equal");
    }
}

Output


Case 1:

Enter the number of row and column of first matrix=3 3

Enter the first matrix of order 3 x 3=

1 2 3

4 5 6

7 8 9

Enter the number of row and column of second matrix=2 3

Enter the second matrix of order 2 x 3=

8 0 5

6 4 1


Matrices are of different order,hence not equal


Case 2:

Enter the number of row and column of first matrix=2 2

Enter the first matrix of order 2 x 2=

1 2

4 5

Enter the number of row and column of second matrix=2 2

Enter the second matrix of order 2 x 2=

1 2

4 5


Matrices are equal


Case 3:

Enter the number of row and column of first matrix=2 2

Enter the first matrix of order 2 x 2=

1 3

5 6

Enter the number of row and column of second matrix=2 2

Enter the second matrix of order 2 x 2=

1 3

5 7


Matrices are not equal. Element mismatch at 2 row 2 column