-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.java
45 lines (37 loc) · 1.22 KB
/
Matrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
public class Matrix {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of rows and columns:");
int r = s.nextInt();
int c = s.nextInt();
if (r != c) {
System.out.println("The matrix is not square, so it cannot be symmetric.");
s.close();
return;
}
int[][] matrix = new int[r][c];
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
matrix[i][j] = s.nextInt();
}
}
s.close();
boolean isSymmetric = true;
outerLoop: // Label to break both loops
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (matrix[i][j] != matrix[j][i]) {
isSymmetric = false;
break outerLoop; // Break both loops
}
}
}
if (isSymmetric) {
System.out.println("The matrix is symmetric.");
} else {
System.out.println("The matrix is not symmetric.");
}
}
}