C program to compare if the two matrices are equal or not

In C programming, comparing two matrices for equality involves checking both their dimensions and corresponding elements. Two matrices are considered equal if they have the same dimensions (rows and columns) and all corresponding elements are identical.

Syntax

// Matrix comparison logic
if (rows1 == rows2 && cols1 == cols2) {
    // Compare each element
    for (int i = 0; i 

Example: Matrix Comparison Program

The following program compares two matrices by checking their dimensions first, then comparing elements if dimensions match −

#include <stdio.h>
#include <stdlib.h>

int main() {
    int A[10][10], B[10][10];
    int i, j, R1, C1, R2, C2, flag = 1;
    
    printf("Enter the order of matrix A (rows columns): ");
    scanf("%d %d", &R1, &C1);
    
    printf("Enter the order of matrix B (rows columns): ");
    scanf("%d %d", &R2, &C2);
    
    printf("Enter elements of matrix A:
"); for(i = 0; i < R1; i++) { for(j = 0; j < C1; j++) { scanf("%d", &A[i][j]); } } printf("Enter elements of matrix B:
"); for(i = 0; i < R2; i++) { for(j = 0; j < C2; j++) { scanf("%d", &B[i][j]); } } printf("\nMatrix A:
"); for(i = 0; i < R1; i++) { for(j = 0; j < C1; j++) { printf("%3d ", A[i][j]); } printf("
"); } printf("\nMatrix B:
"); for(i = 0; i < R2; i++) { for(j = 0; j < C2; j++) { printf("%3d ", B[i][j]); } printf("
"); } /* Check if matrices can be compared */ if(R1 == R2 && C1 == C2) { printf("\nMatrices can be compared
"); /* Compare corresponding elements */ for(i = 0; i < R1; i++) { for(j = 0; j < C1; j++) { if(A[i][j] != B[i][j]) { flag = 0; break; } } if(flag == 0) break; } if(flag == 1) printf("The two matrices are equal
"); else printf("The two matrices are not equal
"); } else { printf("\nMatrices cannot be compared (different dimensions)
"); } return 0; }
Enter the order of matrix A (rows columns): 2 2
Enter the order of matrix B (rows columns): 2 2
Enter elements of matrix A:
1 2
3 4
Enter elements of matrix B:
1 2
3 4

Matrix A:
  1   2 
  3   4 

Matrix B:
  1   2 
  3   4 

Matrices can be compared
The two matrices are equal

How It Works

The program follows these steps −

  • Dimension Check: First compares if both matrices have the same number of rows and columns
  • Element Comparison: If dimensions match, compares each corresponding element
  • Flag Variable: Uses a flag to track equality status during comparison
  • Early Exit: Breaks the loop as soon as a mismatch is found for efficiency

Key Points

  • Two matrices are equal only if they have identical dimensions and all corresponding elements are equal
  • Matrices with different dimensions cannot be compared for equality
  • The algorithm has O(m×n) time complexity where m and n are matrix dimensions

Conclusion

Matrix comparison in C requires checking both dimensional compatibility and element-wise equality. This approach ensures accurate comparison while handling cases where matrices cannot be compared due to size differences.

Updated on: 2026-03-15T14:16:53+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements