C Program to Check Whether Two Matrices are Equal

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a C Program to Check Whether Two Matrices are Equal

Explanation Video:

Custom Image

Explanation:

 

Two matrices are equal if they have the same dimensions and all corresponding elements are identical.

Source Code:
#include <stdio.h>

int main() {
    int arr1[2][2] = {{1, 2}, {3, 4}}, arr2[2][2] = {{1, 2}, {3, 4}};
    int isEqual = 1;

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            if (arr1[i][j] != arr2[i][j]) {
                isEqual = 0;
                break;
            }
        }
    }

    if (isEqual)
        printf("The matrices are equal\n");
    else
        printf("The matrices are not equal\n");

    return 0;
}
Output:
The matrices are equal
Share this Article & Support Us:
Status
printf('Loading...');