C Program to accept two matrices and check if they are equal

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C Program to accept two matrices and check if they are equal */

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

void main()
{
int A[10][10], B[10][10];
int i, j, R1, C1, R2, C2, flag =1;

printf(“Enter the order of the matrix A\n”);
scanf(“%d %d”, &R1, &C1);

printf(“Enter the order of the matrix B\n”);
scanf(“%d %d”, &R2,&C2);

printf(“Enter the elements of matrix A\n”);
for(i=0; i<R1; i++)
{
for(j=0; j<C1; j++)
{
scanf(“%d”,&A[i][j]);
}
}

printf(“Enter the elements of matrix B\n”);
for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
scanf(“%d”,&B[i][j]);
}
}

printf(“MATRIX A is\n”);
for(i=0; i<R1; i++)
{
for(j=0; j<C1; j++)
{
printf(“%3d”,A[i][j]);
}
printf(“\n”);
}

printf(“MATRIX B is\n”);
for(i=0; i<R2; i++)
{
for(j=0; j<C2; j++)
{
printf(“%3d”,B[i][j]);
}
printf(“\n”);
}

/* Comparing two matrices for equality */

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])
{
flag = 0;
break;
}
}
}
}
else
{  printf(” Cannot be compared\n”);
exit(1);
}

if(flag == 1 )
printf(“Two matrices are equal\n”);
else
printf(“But,two matrices are not equal\n”);

}

/*——————————————————
Output
Enter the order of the matrix A
2 2
Enter the order of the matrix B
2 2
Enter the elements of matrix A
1 2
3 4
Enter the elements of matrix B
1 2
3 4
MATRIX A is
1  2
3  4
MATRIX B is
1  2
3  4
Matrices can be compared
Two matrices are equal
——————————————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept two matrices and find the sum and difference of the matrices
  2. C program to read two matrices A (MxN) and B(MxN) and perform addition OR subtraction of A and B. Find the trace of the resultant matrix. Output the given matrix, their sum or Differences and the trace.
  3. C program to read A (MxN), find the transpose of a given matrix and output both the input matrix and the transposed matrix.
  4. C Program to accepts two strings and compare them. Finally it prints whether both are equal, or first string is greater than the second or the first string is less than the second string
  5. Develop functions

Leave a Comment

Previous post:

Next post: