UncleCoder.com

UncleCoder.com

Free programming examples and instructions

# C program to compare two matrices

## C program to compare two matrices and check two matrices are equal or not

Posted on 31 Oct 2017 Category: C Views: 298

Edited on 30 May 2018

C program to compare two matrices

This C Program checks whether the given two Matrices are Equal. The program first reads 2 matrices and then checks whether both the matrices are equal.  Then they need to compare the row and column order of the matrix to check whether it is possible to do an equality check.

If both the matrices are equal then display they are equal. If both the matrices are not equal then display they are different.

Here is source code of the C program to check whether the given two Matrices are Equal or not.

The C program is successfully compiled. The program output is also shown below.

``````	#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
int a[10][10], b[10][10];
int i, j, row1, column1, row2, column2, flag = 1;

printf("Enter the row and column order of the matrix A :\n");
scanf("%d %d", &row1, &column1);
printf("Enter the row and column order of the matrix B :\n");
scanf("%d %d", &row2, &column2);
printf("Enter the elements of matrix A \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
scanf("%d", &a[i][j]);            //reading matrix A elements
}
}
printf("Enter the elements of matrix B \n");
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
scanf("%d", &b[i][j]);       //reading matrix B elements
}
}
printf("MATRIX A  \n");     //display matrix A elements
for (i = 0; i < row1; i++)
{
for (j = 0; j < column1; j++)
{
printf("\t%d\t", a[i][j]);
}
printf("\n");
}
printf("MATRIX B  \n");         //display matrix B elements
for (i = 0; i < row2; i++)
{
for (j = 0; j < column2; j++)
{
printf("\t%d\t", b[i][j]);
}
printf("\n");
}
/*  Comparing two matrices */
if (row1 == row2 && column1 == column2)
{
printf("Matrices can be compared \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < column2; j++)
{
if (a[i][j] != b[i][j])
{
flag = 0;
break;
}
}
}
}
else
{
printf(" Cannot be compared\n");
}
if (flag == 1)
printf("Two matrices are equal \n");
else
printf("Two matrices are not equal \n");
getch();
}
``````

output

• View all

• View all

• View all