Contact Learn C
Copy Program Share Program

Program 129:To compare 2 Matrices

Program 126:
 
#include<stdio.h>
main()
{ 
 int i,j,rows_1,col_1,rows_2,col_2,flag=1;
 printf("Enter number of rows and columns of matrix 1\n");
 scanf("%d %d",&rows_1,&col_1);
 printf("Enter number of rows and columns of matrix 2\n");
 scanf("%d %d",&rows_2,&col_2);
 
 int a1[rows_1][col_1],a2[rows_2][col_2];
 
 if(rows_1==rows_2&&col_1==col_2)
 {
  //Taking input for 1st matrix
     printf("Enter Matrix 1\n");
     for(i=0;i<rows_1;i++)
    {
    for(j=0;j<col_1;j++)
    {
   scanf("%d",&a1[i][j]);
    }
     }
     //Taking input for 2nd matrix
      printf("Enter Matrix 2\n");
     for(i=0;i<rows_2;i++)
    {
    for(j=0;j<col_2;j++)
    {
   scanf("%d",&a2[i][j]);
    }
    }
    
    //Printing given matrices
     printf("Matrix 1  is\n");
     for(i=0;i<rows_1;i++)
    {
    for(j=0;j<col_1;j++)
    {
   printf("%d\t",a1[i][j]);
   
    }
    printf("\n");
    }
    
    printf("Matrix 2  is\n");
     for(i=0;i<rows_2;i++)
    {
    for(j=0;j<col_2;j++)
    {
   printf("%d\t",a2[i][j]);
    }
    printf("\n");
    }
      //Comparing of matrix
    for(i=0;i<rows_1;i++)
    {
     for(j=0;j<col_1;j++)
     {
     if(a1[i][j]!=a2[i][j])
     {
        flag=0;
        }
     }
    }
    
    if(flag==1)
    {
     printf("Both matrices are equal\n");
    }
    else
    {
     printf("Both matrices are not equal\n");
    }
 
     
 }
 else
 {
  printf("Matrices Can not be compared\n");
 }
 
}
Explanation:

 //Coming Soon

 Output:

After 1st run:

compare 2 Matrices







After 2nd run:

compare 2 Matrices



After 3rd run:


compare 2 Matrices
























Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide