Tuesday, 5 September 2017

C-Program to Check Whether Both Matrices are Equal or not



#include<stdio.h>

int main()
{
 int i,j,row1,row2,column1,column2,flag;
 printf("Enter row1\n");
 scanf("%d",&row1);
 printf("Enter column 1\n");
 scanf("%d",&column1);
 printf("Enter row2\n");
 scanf("%d",&row2);
 printf("Enter column 2\n");
 scanf("%d",&column2);
  int a[row1][column1],b[row2][column2];
  if(row1==row2 && column1==column2)
  {
   flag=1;
   printf("Enter Matrix A\n");
      for(i=0;i<row1;i++)
       {
      for(j=0;j<column1;j++)
        scanf("%d",&a[i][j]);
      }
      printf("Enter Matrix B\n");
      for(i=0;i<row2;i++)
       {
      for(j=0;j<column2;j++)
        scanf("%d",&b[i][j]);
      }
     
      for(i=0;i<row1;i++)
      {
       for(j=0;j<column1;j++)
       {
        if(a[i][j]!=b[i][j])
        {
         flag=0;
         break;
        }
       }
      }
      if(flag==1)
     {
      printf("Both Matrices are equal\n");
     }
     else
     {
      printf("Matrices are not Equal\n");
     }
  }
  else
  {
   printf("Not Possible to Compare as Rows and Columns are Not equal\n");
  }    
 return 0;
}

No comments:

Post a Comment