Javatpoint Logo
Javatpoint Logo

Program to determine whether two matrices are equal

Explanation

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:

  1. Both the matrices should have the same number of rows and columns.
  2. Both the matrices should have the same corresponding elements.
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

  1. Declare and initialize two two-dimensional arrays a and b.
  2. Calculate the number of rows and columns present in the array a and store it in variables row1 and col1 respectively.
  3. Calculate the number of rows and columns present in array b and store it in variables row2 and col2 respectively.
  4. Initialize variable flag to true.
  5. Check if the size of both the arrays is equal. If arrays are not of equal size then, display the message " Matrices are not equal".
  6. If the size of both the arrays is equal then, loop through both the arrays and compare each element.
  7. If any of the corresponding elements are not equal then, set the flag to false and break the loop.
  8. If the flag is equal to true which implies matrices are equal. Else, matrices are not equal.

Solution

Python

Output:

Matrices are equal

C

Output:

Matrices are equal

JAVA

Output:

Matrices are equal

C#

Output:

Matrices are equal

PHP

Output:

Matrices are equal

Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials