- Write a C program to compare two matrices for equality.
- How to check if two matrix are equal.

Given two matrices, we have to write a program in c to check whether these two matrices are equal or not. Below program takes two matrices as input from user and compares them for equality. If both matrices are equal, it will print "EQUAL MATRICES" on screen otherwise "UNEQUAL MATRICES".

**Equality conditions of two matrices**

*Let A and B are two matrices of dimension M x N. Matrix A and B are said to be equal if and only If below mentioned conditions are satisfied:*

- The dimensions of both matrices must be same. If size of matrix A is m x n, then size of matrix B must also be m x n.
- The value of any element A[i][j] must be equal to the value of corresponding element in matrix B, that is B[i][j].

A[i,j] = B[i,j] such that 1 <= i <= m and i <= j <= n.

**Algorithm to check whether two matrices are equal or not**

*Let A and B are two matrices of dimension M x N.*

- First of all, number of rows and columns of both matrices must be same.
- Traverse both matrices row wise(first all elements of a row from left to right, then jump to next row) using two loops(check line number 38 and 39 of below program).
- Compare every element of matrix A(A[i][j]) with corresponding element in matrix B(B[i][j]). If they are equal then continue otherwise A and B are unequal matrices.
- After traversing whole array, If you didn't found any inequality in elements of both matrices at any position [i,j], then both matrices are equal.

## C program to check equality of two matrices

/* * C Program to compare two matrices */ #include <stdio.h> #include <conio.h> int main(){ int rows1, cols1, rows2, cols2, rowCounter, colCounter, isEqual = 1; int firstMatrix[50][50], secondMatrix[50][50]; /* Input first matrix*/ printf("Enter size of first matrix\n"); scanf("%d %d", &rows1, &cols1); printf("Enter first matrix of size %dX%d\n", rows1, cols1); for(rowCounter = 0; rowCounter < rows1; rowCounter++){ for(colCounter = 0; colCounter < cols1; colCounter++){ scanf("%d", &firstMatrix[rowCounter][colCounter]); } } /* Input second matrix*/ printf("Enter size of second matrix\n"); scanf("%d %d", &rows2, &cols2); printf("Enter second matrix of size %dX%d\n", rows2, cols2); for(rowCounter = 0; rowCounter < rows2; rowCounter++){ for(colCounter = 0; colCounter < cols2; colCounter++){ scanf("%d", &secondMatrix[rowCounter][colCounter]); } } /* Compare size of both matrices. */ if((rows1 != rows2) || (cols1 != cols2)){ printf("UNEQUAL MATRICES: Size of both matrices not same\n"); } else { for(rowCounter=0; rowCounter<rows1 && isEqual==1; rowCounter++){ for(colCounter = 0; colCounter < cols1; colCounter++){ if(firstMatrix[rowCounter][colCounter] != secondMatrix[rowCounter][colCounter]){ printf("UNEQUAL MATRICES: Element mismatch\n"); isEqual = 0; break; } } } if(isEqual == 1){ printf("EQUAL MATRICES\n"); } } getch(); return 0; }

**Program Output**

Enter size of first matrix 2 2 Enter first matrix of size 2X2 1 2 3 4 Enter size of second matrix 1 2 Enter second matrix of size 1X2 5 6 UNEQUAL MATRICES: Size of both matrices not same

Enter size of first matrix 2 2 Enter first matrix of size 2X2 1 2 3 4 Enter size of second matrix 2 2 Enter second matrix of size 1X2 1 1 1 1 UNEQUAL MATRICES: Element mismatch

Enter size of first matrix 2 2 Enter first matrix of size 2X2 1 2 3 4 Enter size of second matrix 2 2 Enter second matrix of size 1X2 1 2 3 4 EQUAL MATRICES

**Related Topics**