Check Weather two Matrices are Identical

Check Weather two Matrices are Identical:


Program:

import java.util.Scanner;

public class CheckIdenticalMatrices {

public static void main(String[] args) {

int r1, c1, r2, c2;

Scanner s = new Scanner(System.in);

r1 = s.nextInt();

c1 = s.nextInt();

r2 = s.nextInt();

c2 = s.nextInt();

if(r1 != r2 || c1!=c2) {

System.out.println("Matrices are not identical!");

}

int[][] matrix1 = new int[r1][c1];

int[][] matrix2 = new int[r2][c2];

for(int i = 0; i<r1; i++) {

for(int j = 0; j<c1;j++) {

matrix1[i][j] = s.nextInt();

}

}

for(int i = 0; i<r2; i++) {

for(int j = 0; j<c2;j++) {

matrix2[i][j] = s.nextInt();

}

}

boolean flag = true;

for(int i = 0; i<r1; i++) {

for(int j = 0;j<c1; j++) {

if(matrix1[i][j] != matrix2[i][j]) {

flag = false;

break;

}

}

if(flag == false) {

break;

}

}

if(flag == true) {

System.out.println("Matrices are Identical :)");

}

else {

System.out.println("Matrices are not Identical :(");

}

}

}


Output:

Case 1:


            


Case 2:

          




                        Happy Coding 💻 :-)




Comments

Popular posts from this blog

Valid Username Regular Expression

Rotate String in Right Direction in java

Sum of Elements of Lower Triangular Matrix