- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java program to check if two given matrices are identical

Two matrices are identical if their number of rows and columns are equal and the corresponding elements are also equal. An example of this is given as follows.

Matrix A = 1 2 3 4 5 6 7 8 9 Matrix B = 1 2 3 4 5 6 7 8 9 The matrices A and B are identical

A program that checks if two matrices are identical is given as follows.

## Example

public class Example { public static void main (String[] args) { int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} }; int B[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} }; int flag = 1; int n = 3; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (A[i][j] != B[i][j]) flag = 0; if (flag == 1) System.out.print("Both the matrices are identical"); else System.out.print("Both the matrices are not identical"); } }

## Output

Both the matrices are identical

Now let us understand the above program.

The two matrices A and B are defined. The initial value of flag is 1. Then a nested for loop is used to compare each element of the two matrices. If any corresponding element is not equal, then value of flag is set to 0. The code snippet that demonstrates this is given as follows −

int A[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} }; int B[][] = { {7, 9, 2}, {3, 8, 6}, {1, 4, 2} }; int flag = 1; int n = 3; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (A[i][j] != B[i][j]) flag = 0;

If flag is 1, then matrices are identical and this is displayed. Otherwise, the matrices are not identical and this is displayed. The code snippet that demonstrates this is given as follows −

if (flag == 1) System.out.print("Both the matrices are identical"); else System.out.print("Both the matrices are not identical");

- Related Articles
- Python Program to check if two given matrices are identical
- Program to check if two given matrices are identical in C++
- C# program to check if two matrices are identical
- Check if two lists are identical in Python
- How to check if two matrices are equal in R?
- Python program to check whether two lists are circularly identical
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check Whether Two Matrices Are Equal or Not
- Java Program to check if two dates are equal
- Java Program to Check if two strings are anagram
- Java program to add two matrices.
- Java program to subtract two matrices.
- Java program to multiply two matrices.
- Check if two list of tuples are identical in Python
- C++ Program to Check Multiplicability of Two Matrices

Advertisements