Skip to content
Home » Java Programs » Java Program To Check If Two Matrices Are Equal

Java Program To Check If Two Matrices Are Equal

Basic java program to check if two matrices are equal. In this program, we can check the matrix is equal or not in java. If all three of the following conditions are met, two matrices are equal: The number of rows in each matrix is the same. The number of columns in each matrix is the same. Each matrix’s corresponding elements are all the same.

Matrix Definition in Java

A matrix is a set of integers organised into a predetermined number of rows and columns. These are usually real numbers. A two-dimensional array has two dimensions, one for each row and one for each column. If you supply an integer array int arr[4][4], the matrix will have 4 rows and 4 columns.

A matrix’s numbers can be used to represent both data and mathematical equations. They’re asked to multiply matrices even more frequently. Solving linear equations for specific variables can be thought of as matrix multiplication. java program to check if two matrices are equal to check the matrix is equal or not in java.

They are used to plot graphs, calculate statistics, and conduct scientific studies and research in a variety of fields. Matrices are also used to represent real-world data such as population, infant mortality rate, and so on. They are the most accurate survey plotting methods.

For Loop in Java

In Java, the “for” loop is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a set number of times. The test-condition specified inside the “for” loop determines the number of iterations. The Java “for” loop is one of the most simple Java loops to grasp. 

Flag Value in Java

A flag is a binary value that is stored in a predetermined bit or bit sequence. A flag is typically used by a program to remember something or to leave a message for another program. 

A flag variable is a source data variable that has been identified as a way to signal the clinical relevance of lab test or vital sign findings, or to indicate whether treatment-emergent adverse events are occurring. 

Java Program To Check If Two Matrices Are Equal

//java program to check if two matrices are equal
public class Main 
{ 
public static void main(String[] args)
{ 
int row1, col1, row2, col2; 
boolean flag = true; 
int a[][] = { 
{2, 4, 6}, 
{8, 4, 6}, 
{4, 5, 7} 
}; 
int b[][] = { 
{2, 4, 6}, 
{8, 4, 6}, 
{4, 5, 7} 
}; 
row1 = a.length; 
col1 = a[0].length; 
row2 = b.length; 
col2 = b[0].length; 
if(row1 != row2 || col1 != col2){ 
System.out.println("Matrix are not equal"); 
} 
else { 
for(int i = 0; i < row1; i++)
{ 
for(int j = 0; j < col1; j++)
{ 
if(a[i][j] != b[i][j])
{ 
flag = false; 
break; 
} 
} 
} 
if(flag) 
System.out.println("Matrices are equal"); 
else 
System.out.println("Matrices are not equal"); 
} 
} 
} 

Output:

Matrices are equal