Write C Program to check whether two matrices are equal or not


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


  1. #include <stdio.h>
  2.  
  3. #define size 2 // Matrix size declaration
  4.  
  5. int main()
  6. {
  7. int A[size][size];
  8. int B[size][size];
  9.  
  10. int row, col, isEqual;
  11.  
  12. // Input elements in first matrix from user
  13. printf("Enter elements in matrix A of size %d x %d: \n", size, size);
  14. for(row=0; row<size; row++)
  15. {
  16. for(col=0; col<size; col++)
  17. {
  18. scanf("%d", &A[row][col]);
  19. }
  20. }
  21.  
  22. // Input elements in second matrix from user
  23. printf("\nEnter elements in matrix B of size %d x %d: \n", size, size);
  24. for(row=0; row<size; row++)
  25. {
  26. for(col=0; col<size; col++)
  27. {
  28. scanf("%d", &B[row][col]);
  29. }
  30. }
  31.  
  32. // Assumes that the matrices are equal
  33. isEqual = 1;
  34.  
  35. for(row=0; row<size; row++)
  36. {
  37. for(col=0; col<size; col++)
  38. {
  39.  
  40. //If the corresponding entries of matrices are not equal
  41.  
  42. if(A[row][col] != B[row][col])
  43. {
  44. isEqual = 0;
  45. break;
  46. }
  47. }
  48. }
  49.  
  50. /*
  51.   * Checks the value of isEqual
  52.   * As per our assumption if isEqual contains 1 means both are equal
  53.   * If it contains 0 means both are not equal
  54.   */
  55. if(isEqual == 1)
  56. {
  57. printf("\nMatrix A is equal to Matrix B");
  58. }
  59. else
  60. {
  61. printf("\nMatrix A is not equal to Matrix B");
  62. }
  63.  
  64. return 0;
  65. }
  66.  

Result

C language techstudy.org

     






Related Topics




Popular Posts