

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to compare if the two matrices are equal or not
The user has to enter the order of two matrices and elements of two matrices. Then, these two matrix are compared.
If both matrix elements and size are equal, then it displays that the two matrices are equal.
If size of matrix is equal but the elements are not equal, then it displays that the matrix can be compared but is not equal.
If the size and elements are not matched, then it displays that the matrices cannot be compared.
Program
Following is the C program to compare if the two matrices are equal or not −
#include <stdio.h> #include <conio.h> 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
When the above program is executed, it produces the following result −
Run 1: 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 Run 2: 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 5 6 7 8 MATRIX A is 1 2 3 4 MATRIX B is 5 6 7 8 Matrices can be compared But,two matrices are not equal
- Related Questions & Answers
- Check if two StringDictionary objects are equal or not in C#
- C# program to check if two matrices are identical
- How to check if two matrices are equal in R?
- Check if two strings are equal or not in Arduino
- Compare and return True if two string Numpy arrays are not equal
- C Program to check if two strings are same or not
- Program to check if two given matrices are identical in C++
- Python Program to check if two given matrices are identical
- Java program to check if two given matrices are identical
- Python Pandas - Check if the dataframe objects are equal or not
- Python Pandas - Determine if two Index objects with opposite orders are equal or not
- Java Program to check if two dates are equal
- Check whether two schedules are view equal or not(DBMS)
- C# program to check whether two sequences are the same or not
- C program to verify if the numbers are abundant(friendly) or not?
Advertisements