C Program to Check Whether Two Matrices are Equal or Not

Howdy readers, today you will learn how to write a program to check whether two matrices are equal or not using C Programming language.

C Program to Check Whether Two Matrices are Equal or Not

Two matrices are called equal matrices if they have the same order or dimension and the corresponding elements are equal. Suppose A and B are the matrices of the order i x j and aij = bij, then A and B are equal matrices. For example:

A = 1  2   =   1  2   =  B
    3  4       3  4

Logic to Check Two Matrices are equal or not: 

  1. To check whether two matrices are equal or not, we will first check the order of the both matrices.
  2. Then we create a nested list to check the corresponding elements of both the elements.
  3. If all the corresponding elements are equal, then both the matrices are equal.

So, without any delay, let’s begin this tutorial.

C Program to Check Whether Two Matrices are Equal or Not

C Program

// C Program to Check Whether Two Matrices are Equal or Not
#include <stdio.h>
int main(){
    int i, j, r1, c1, r2, c2, isEqual = 1;
    int a[10][10], b[10][10];
    
    // Asking for input
    printf("Enter the rows and columns of first matrix: \n");
    scanf("%d %d", &r1, &c1);
    printf("Enter the rows and columns of second matix: \n");
    scanf("%d %d", &r2, &c2);
    
    // First matrix elements
    printf("Enter elements of first matrix: \n");
    for (i = 0; i < r1; i++){
        for (j = 0; j < c1; j++){
            scanf("%d", &a[i][j]);
        }
    }
    
    // Second matrix elements
    printf("Enter elements of second matrix: \n");
    for (i = 0; i < r2; i++){
        for (j = 0; j < c2; j++){
            scanf("%d", &b[i][j]);
        }
    }
    
    // Comparing two matrices
    if (r1 == r2 && c1 == c2){
        printf("Matrices can be compared.\n");
        for (i = 0; i < r1; i++){
            for (j = 0; j < c2; j++){
                if (a[i][j] != b[i][j]){
                    isEqual = 0;
                    break;
                }
            }
        }
    }
    else{
        printf("Matrices cannot be compared.");
    }
    
    if (isEqual == 1){
        printf("Two Matrices are equal.");
    }
    else{
        printf("Two Matrices are not equal.");
    }
    
    return 0;
}

Output 1

Enter the rows and columns of first matrix: 
2 2
Enter the rows and columns of second matix: 
2 2
Enter elements of first matrix: 
4 6
7 5
Enter elements of second matrix: 
4 6
7 5
Matrices can be compared.
Two Matrices are equal.

Output 2

Enter the rows and columns of first matrix: 
3 3
Enter the rows and columns of second matix: 
3 3
Enter elements of first matrix: 
1 3 5
2 4 6
5 3 8
Enter elements of second matrix: 
1 5 3
4 5 6
8 6 5
Matrices can be compared.
Two Matrices are not equal.
C Program to Check Whether Two Matrices are Equal or Not

Explanation

    int i, j, r1, c1, r2, c2, isEqual = 1;
    int a[10][10], b[10][10];

In this program, we have declared 7 int data type variables and 2 arrays.

    // Asking for input
    printf("Enter the rows and columns of first matrix: \n");
    scanf("%d %d", &r1, &c1);
    printf("Enter the rows and columns of second matix: \n");
    scanf("%d %d", &r2, &c2);
    
    // First matrix elements
    printf("Enter elements of first matrix: \n");
    for (i = 0; i < r1; i++){
        for (j = 0; j < c1; j++){
            scanf("%d", &a[i][j]);
        }
    }
    
    // Second matrix elements
    printf("Enter elements of second matrix: \n");
    for (i = 0; i < r2; i++){
        for (j = 0; j < c2; j++){
            scanf("%d", &b[i][j]);
        }
    }

Then, the user is asked to enter the order and elements of the matrices.

    // Comparing two matrices
    if (r1 == r2 && c1 == c2){
        printf("Matrices can be compared.\n");
        for (i = 0; i < r1; i++){
            for (j = 0; j < c2; j++){
                if (a[i][j] != b[i][j]){
                    isEqual = 0;
                    break;
                }
            }
        }
    }

Now, we create a nested loop to check whether the corresponding elements of both the matrices are equal or not.

If they are unequal, then isEqual becomes 0 and the loop terminates.

    if (isEqual == 1){
        printf("Two Matrices are equal.");
    }
    else{
        printf("Two Matrices are not equal.");
    }

If the value of isEqual is 1, then the two matrices are equal. Otherwise the two matrices are not equal.

Conclusion

I hope after going through this post, you understand how to write a program to check whether two matrices are equal or not using C Programming language.

If you have any doubt regarding the program, then let us know in the comment section. We will be delighted to solve your query.

Also Read:

Leave a Comment