# Determine Whether Two Matrices Are Equal in Java

Matrix operations are fundamental in programming and data analysis, and one common task is determining whether two matrices are equal. In Java, you can accomplish this by developing a method to compare matrices. In this comprehensive guide, we will explore two key methods for achieving this:

• Taking user input
• Matrices are equal

## Taking user input

Before we dive into matrix comparison, we need to create the matrices we want to evaluate. This requires gathering user input for the matrices. We’ll walk through the process of taking user input, including matrix dimensions and elements, using Java.

```// Determine Whether Two Matrices Are Equal in Java
import java.util.Scanner;
public class MatrixEqualEx{
public static void main(String args[]){
//System.in is a standard input stream
Scanner sc= new Scanner(System.in);
int a,b,x,y,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=");
a=sc.nextInt();
b=sc.nextInt();

//declare first matrix
int A[][]=new int[a][b];
//take input of the first matrix
System.out.print("Enter the first matrix of order "+a+" x "+b+"=\n");
for(i=0;i<a;i++){
for(j=0;j<b;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=");
x=sc.nextInt();
y=sc.nextInt();
//declare second matrix
int B[][]=new int[x][y];
//take input of the second matrix
System.out.print("Enter the second matrix of order "+x+" x "+y+"=\n");
for(i=0;i<x;i++){
for(j=0;j<y;j++){
B[i][j]=sc.nextInt();
}
}

// check if order of matrices are same
if(a!=x||b!=y){
System.out.print("\nMatrices are of different order,hence not equal");
flag=1;
}
else{
//check equality of each corresponding elements
for(i=0;i<a;i++){
for(j=0;j<b;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");
}
}```

Also, learn about  Multiply Two Matrices in Java, Now!

This Java program, “Determine Whether Two Matrices Are Equal,” allows users to input two matrices and check if they are equal. Here’s an explanation of the code:

1. The program starts by importing the `java.util.Scanner` class to facilitate user input.

2. Users are prompted to input the order (number of rows and columns) of the first matrix, and the dimensions are stored in variables `a` and `b`.

3. The first matrix `A` is declared as a 2D array with dimensions determined by `a` and `b`.

4. Users then input the elements for the first matrix, filling it row by row.

5. Similarly, users input the order of the second matrix, and the dimensions are stored in variables `x` and `y`.

6. The second matrix `B` is declared with dimensions based on `x` and `y`.

7. Users input the elements for the second matrix in a similar manner.

8. The program checks if the orders of the two matrices are the same (i.e., if `a` equals `x` and `b` equals `y`). If they are different, the matrices cannot be equal, and a message is displayed, setting the `flag` variable to 1.

9. If the matrix orders are the same, the program proceeds to compare each corresponding element of the matrices. If any inequality is detected, it prints a message indicating where the mismatch occurs and sets the `flag` to 1.

10. If no mismatches are found and the `flag` remains 0, the program concludes that the matrices are equal and displays a corresponding message.

#### Output:

``````Enter the number of row and column of first matrix=2 2
Enter the first matrix of order 2 x 2=
2 4 6 8
Enter the number of row and column of second matrix=2 2
Enter the second matrix of order 2 x 2=
2 4 6 8
Matrices are equal``````

## Defined matrix not taking input from the user

Below provided Java program aims to determine whether two matrices, `x` and `y`, are equal.

Learn How to Add Two Matrices in Java, Here!

```// Determine Whether Two Matrices Are Equal in Java
import java.util.Scanner;
public class EqualMatrixEx
{
public static void main(String[] args) {
int row1, col1, row2, col2;
boolean flag = true;

//Initialize matrix x
int x[][] = {
{6, 8, 5},
{1, 2, 4},
{3, 0, 7}
};

//Initialize matrix y
int y[][] = {
{9, 4, 6},
{2, 8, 3},
{1, 5, 7}
};

row1 = x.length;
col1 = x[0].length;

//Calculates the number of rows and columns present in the second matrix

row2 = y.length;
col2 = y[0].length;

//Checks if dimensions of both the matrices are equal
if(row1 != row2 || col1 != col2){
System.out.println("Matrices are not equal");
}
else {
for(int i = 0; i < row1; i++){
for(int j = 0; j < col1; j++){
if(x[i][j] != y[i][j]){
flag = false;
break;
}
}
}

if(flag)
System.out.println("Matrices are equal");
else
System.out.println("Matrices are not equal");
}
}
}```

Explanation of the code:
It does so by first initializing the matrices with predefined values. Then, it calculates the number of rows and columns in each matrix and compares their dimensions. If the dimensions are not equal, the program concludes that the matrices are unequal.

However, if the dimensions match, the program enters a nested loop to compare each element of the two matrices. If at any point, an element in matrix `x` is not equal to the corresponding element in the matrix `y`, a flag is set to `false`, and the loop is terminated prematurely. This indicates that the matrices are not equal.

On the other hand, if the program successfully compares all corresponding elements without finding any differences, the flag remains `true`, signifying that the matrices are equal. Finally, based on the flag’s value, the program prints the appropriate message indicating whether the matrices are equal or not.

#### Output:

``Matrices are not equal``

In this Java program, we’ve demonstrated how to determine the equality of two matrices. It starts by taking user input for the matrices’ elements and dimensions. If the matrices are equal, it rightly concludes that “Matrices are equal,” offering a useful tool for matrix comparison in Java.