-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmatrix_symmetric.java
52 lines (46 loc) · 1.6 KB
/
matrix_symmetric.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
46
47
48
49
50
51
52
/** Q3 Java program to check whether a matrix is symmetric or not. */
import java.util.Scanner;
public class matrix_symmetric {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of rows: ");
int rows = sc.nextInt();
System.out.println("Enter no. of columns: ");
int cols = sc.nextInt();
int matrix[][] = new int[rows][cols];
System.out.println("Enter elements: ");
for (int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println("Printing the input matrix: ");
for (int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
if(rows != cols) {
System.out.println("The given matrix is not a square matrix, so it is not symmetric.");
}
else {
boolean symmetric = true;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(matrix[i][j] != matrix[j][i]) {
symmetric = false;
break;
}
}
}
if(symmetric) {
System.out.println("The given matrix is symmetric...");
}
else {
System.out.println("The given matrix is not symmetric...");
}
}
sc.close();
}
}