Java Program to determine whether two matrices are equal

In this program, we need to check whether given matrices are equal or not.

Two matrices are said to be equal if and only if they satisfy the following conditions:

  • Both the matrices should have the same number of rows and columns.
  • Both the matrices should have the same corresponding elements.
Java Program to determine whether two matrices are equal

Consider the above example, where matrices A and B are equal as they have the same size and same corresponding elements.

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE row1, col1, row2, col2
  • STEP 3: INITIALIZE first matrix a[][] ={{1, 2, 3}, {8, 4,6}, {4, 5,7}}
  • STEP 4: INITIALIZE second matrix b[][] ={{1 2, 3}, {8, 4, 6}{4, 5, 7}}
  • STEP 5: row1 = a.length
  • STEP 6: col1 = a[0].length
  • STEP 7: row2 =b.length
  • STEP 8: col2 = b[0].length
  • STEP 9: if(row1!=row2 || col1!=col2)
            then
            PRINT “No”
            else
            go to step 10;
  • STEP 10: REPEAT STEP 11 UNTIL i<row1
            //for(i=0; i<row1; i++)
  • STEP 11: REPEAT STEP12 UNTIL j<col1
            //for(j=0; j<col1; j++)
  • STEP 12: if(a[i][j]=b[i][j]) then
            flag =false
            break
  • STEP 13: if(flag)
            then PRINT “Yes”
            else
            PRINT “No”
  • STEP 14: END

program

  1. public class EqualMatrix    
  2. {    
  3.     public static void main(String[] args) {    
  4.         int row1, col1, row2, col2;    
  5.         boolean flag = true;    
  6.             
  7.         //Initialize matrix a    
  8.         int a[][] = {       
  9.                         {1, 2, 3},    
  10.                         {8, 4, 6},    
  11.                         {4, 5, 7}    
  12.                     };    
  13.               
  14.           //Initialize matrix b    
  15.         int b[][] = {       
  16.                         {1, 2, 3},    
  17.                         {8, 4, 6},    
  18.                         {4, 5, 7}    
  19.             };    
  20.               
  21.         //Calculates the number of rows and columns present in the first matrix    
  22.     
  23.           row1 = a.length;    
  24.         col1 = a[0].length;    
  25.             
  26.         //Calculates the number of rows and columns present in the second matrix    
  27.     
  28.           row2 = b.length;    
  29.         col2 = b[0].length;    
  30.             
  31.         //Checks if dimensions of both the matrices are equal    
  32.         if(row1 != row2 || col1 != col2){    
  33.             System.out.println(“Matrices are not equal”);    
  34.         }    
  35.         else {    
  36.             for(int i = 0; i < row1; i++){    
  37.                 for(int j = 0; j < col1; j++){    
  38.                   if(a[i][j] != b[i][j]){    
  39.                       flag = false;    
  40.                       break;    
  41.                   }    
  42.                 }    
  43.             }    
  44.                 
  45.             if(flag)    
  46.                 System.out.println(“Matrices are equal”);    
  47.             else    
  48.                 System.out.println(“Matrices are not equal”);    
  49.         }    
  50.     }    
  51. }    

Output:

Matrices are equal

Leave a Comment