BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to check whether two matrices are equal

Last Updated: February 10, 2022 by Chaitanya Singh | Filed Under: Java Examples

Here we will write a java program to determine whether two matrices are equal or not. Two matrices are said to be equal if the dimensions (row and column count) of both the matrices are same and all the corresponding elements are equal. For example if two matrices m1 & m2 are equal then dimensions of m1 & m2 should be same and all the elements should match, m1[0][0] = m2[0][0], m1[0][1] = m2[0][1] and so on.

Program to determine whether two matrices are equal

Logic that we are following in the program below is:

if row and column count of m1 & m2 doesn’t match, no need to do further processing as we can simply say that matrices are not equal.

if row and column count matches then we are checking each element one by one and if any of the element doesn’t match we are storing the boolean value “false” in the boolean variable flag. Later we are displaying “Given matrices are not equal” if the flag value is false.

public class JavaExample
{
    public static void main(String[] args) {
        int rowCount1, colCount1, rowCount2, colCount2;
        boolean flag = true;

        //Input matrix m1
        int m1[][] = {
                {19, 8, 17},
                {6, 14, 16},
                {4, 15, 17}
        };

        //Input matrix m2
        int m2[][] = {
                {19, 8, 17},
                {6, 14, 16},
                {4, 15, 17}
        };

        // Finding row and column count for matrix m1
        rowCount1 = m1.length;
        colCount1 = m1[0].length;

        // Finding row and column count for matrix m2
        rowCount2 = m2.length;
        colCount2 = m2[0].length;

        //Checking if size of both the matrices m1 & m2are equal
        if(rowCount1 != rowCount2 || colCount1 != colCount2){
            System.out.println("Given matrices are not equal");
        }
        else {
            // Here we are matching each element of m1 & m2 if
            // any element doesn't match we are setting the flag to false
            for(int i = 0; i < rowCount1; i++){
                for(int j = 0; j < colCount1; j++){
                    if(m1[i][j] != m2[i][j]){
                        flag = false;
                        break;
                    }
                }
            }

            //If flag is not true it means the matrices are not equal
            if(flag)
                System.out.println("Given matrices are equal");
            else
                System.out.println("Given matrices are not equal");
        }
    }
}    

Output:

Given matrices are equal
❮ Java Programs

Top Related Articles:

  1. Split String into array of Characters in Java
  2. Java Program to Calculate Power of a Number
  3. Java Program to Print Pyramid Star Pattern
  4. Java Program to Print Left Triangle Star Pattern
  5. Java Program to Print Right Triangle Star Pattern

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap