Program to check two matrix are equal or not

Problem Statement:- Program to accept two matrices and check whether they are equal or Not.

Sample Input/Output:-


Sample Input First:2 2 2 2

4 5 6 7


4 5 6 7

Sample Output First: 

Two matrices are equal.

Sample Input Second: 2 2 3 2

7 8 9 3


7 2 1 3 5 6

Sample Output Second: 

Two matrices are not equal.


Data requirement:-


  Input Data:- row_size, col_size, matrix[][]


  Output Data:-String output


  Additional Data:- i, j


Program in C

Here is the source code of the C Program to check two matrices are equal or not.


Code:


#include<stdio.h> #include <stdlib.h> int main() { int row_size,col_size; //Get size of 1st matrix printf("Enter the row Size Of the 1st Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the 1st Matrix:"); scanf("%d",&col_size); //Get size of 2nd matrix int row_size1,col_size1; printf("Enter the row Size Of the 2nd Matrix:"); scanf("%d",&row_size1); printf("Enter the columns Size Of the 2nd Matrix:"); scanf("%d",&col_size1); int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; printf("Enter the 1st Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } int matrix1[row_size1][col_size1]; //Taking input of the 2nd matrix printf("Enter the 2nd Matrix Element:\n"); for(i=0;i<row_size1;i++) { for(j=0;j<col_size1;j++) { scanf("%d",&matrix1[i][j]); } } //Compare two matrices int point=0; if(row_size==row_size1 && col_size==col_size1) { for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]!=matrix1[i][j]) { point=1; break; } }} } else { printf("Two matrices are not equal."); exit(0); } if(point==1) printf("Two matrices are not equal."); else printf("Two matrices are equal."); }


Input/Output:

Enter the row Size Of the 1st Matrix:2 Enter the columns Size Of the 1st Matrix:2 Enter the row Size Of the 2nd Matrix:2 Enter the columns Size Of the 2nd Matrix:2 Enter the 1st Matrix Element: 4 5 6 7 Enter the 2nd Matrix Element: 4 5 6 7 Two matrices are equal.


Program in C++

Here is the source code of the C++ Program to accept two matrices and check whether they are equal or Not.


Code:


#include<iostream> #include <stdlib.h> using namespace std; int main() { int row_size,col_size; //Get size of 1st matrix cout<<"Enter the row Size Of the 1st Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the 1st Matrix:"; cin>>col_size; //Get size of 2nd matrix int row_size1,col_size1; cout<<"Enter the row Size Of the 2nd Matrix:"; cin>>row_size1; cout<<"Enter the columns Size Of the 2nd Matrix:"; cin>>col_size1; int matrix[row_size][col_size]; //Taking input of the 1st matrix int i,j; cout<<"Enter the 1st Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } int matrix1[row_size][col_size]; //Taking input of the 2nd matrix cout<<"Enter the 2nd Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix1[i][j]; } } //Compare two matrices int point=0; if(row_size==row_size1 && col_size==col_size1) { for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]!=matrix1[i][j]) { point=1; break; } }} } else { cout<<"Two matrices are not equal."; exit(0); } if(point==1) cout<<"Two matrices are not equal."; else cout<<"Two matrices are equal."; }


Input/Output:

Enter the row Size Of the 1st Matrix:2 Enter the columns Size Of the 1st Matrix:2 Enter the row Size Of the 2nd Matrix:3 Enter the columns Size Of the 2nd Matrix:2 Enter the 1st Matrix Element: 7 8 9 3 Enter the 2nd Matrix Element: 7 2 1 3 5 6 Two matrices are not equal.


Program in Java

Here is the source code of the Java Program to check two matrices are equal or not.


Code:


import java.util.Scanner; public class CompareTwoMatrices { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of 1st matrix System.out.print("Enter the row Size Of the 1st Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the 1st Matrix:"); col_size=cs.nextInt(); //Get size of 2nd matrix int row_size1,col_size1; System.out.print("Enter the row Size Of the 2nd Matrix:"); row_size1=cs.nextInt(); System.out.print("Enter the columns Size Of the 2nd Matrix:"); col_size1=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of 1st the matrix int i,j; System.out.println("Enter the 1st Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } int matrix1[][] =new int[row_size1][col_size1]; //Taking input of 2nd the matrix System.out.println("Enter the 2nd Matrix Element:"); for(i=0;i<row_size1;i++) { for(j=0;j<col_size1;j++) { matrix1[i][j]=cs.nextInt(); } } //Compare two matrices int point=0; if(row_size==row_size1 && col_size==col_size1) { for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { if(matrix[i][j]!=matrix1[i][j]) { point=1; break; } }} } else { System.out.print("Two matrices are not equal."); System.exit(0); } if(point==1) System.out.print("Two matrices are not equal."); else System.out.print("Two matrices are equal."); cs.close(); } }


Input/Output:

Enter the row Size Of the 1st Matrix:3 Enter the columns Size Of the 1st Matrix:3 Enter the row Size Of the 2nd Matrix:3 Enter the columns Size Of the 2nd Matrix:3 Enter the 1st Matrix Element: 4 5 6 7 8 9 2 3 4 Enter the 2nd Matrix Element: 1 2 3 4 5 6 7 8 9 Two matrices are not equal.

Program in Python

Here is the source code of the Python Program to accept two matrices and check whether they are equal or Not.


Code:


# Get size of 1st matrix row_size=int(input("Enter the row Size Of the 1st Matrix:")) col_size=int(input("Enter the columns Size Of the 1st Matrix:")) # Get size of 2nd matrix row_size1=int(input("Enter the row Size Of the 1st Matrix:")) col_size1=int(input("Enter the columns Size Of the 2nd Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the 1st Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) matrix1=[] # Taking input of the 2nd matrix print("Enter the 2nd Matrix Element:") for i in range(row_size): matrix1.append([int(j) for j in input().split()]) # Compare two matrices point=0 if row_size==row_size1 and col_size==col_size1: for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j] != matrix1[i][j]: point=1 break else: print("Two matrices are not equal.") exit(0) if point==1: print("Two matrices are not equal.") else: print("Two matrices are equal.")


Input/Output:

Enter the row Size Of the 1st Matrix:2 Enter the columns Size Of the 1st Matrix:3 Enter the row Size Of the 1st Matrix:2 Enter the columns Size Of the 2nd Matrix:3 Enter the 1st Matrix Element: 1 2 3 4 5 6 Enter the 2nd Matrix Element: 1 2 3 4 5 6 Two matrices are equal.



Most Recommend Questions:-








More Questions:-




Array Programming Questions and Solutions(50)

C/C++/Java/Python Practice Question

Post a Comment

0 Comments