# Symmetric Matrix Program in Java

In this article, you will learn how to find a Matrix that is Symmetric Matrix or not using Java. A symmetric matrix is a square matrix that remains unchanged when transposed (rows become columns, and vice versa).

Symmetric Matrix: A symmetric matrix is a square matrix where the element at the ith row and jth column is equal to the element at the jth row and ith column. In other words, if A is a symmetric matrix, then A[i][j] = A[j][i] for all i and j. Consequently, the matrix A is symmetric if its transpose is equal to itself, i.e., A^T = A.

For example, consider the following symmetric matrix:

1 2 3
2 4 5
3 5 6

Here, the element at row 1, column 2 (2) is the same as the element in row 2, column 1 (2), and so on.

In Java, symmetric matrices are often represented using a 2D array. The 2D array stores the elements of the matrix, and to check whether the matrix is symmetric, we need to compare each element with its corresponding transposed element. Here’s a Java program to check if a given square matrix is an identity matrix:

```package com.javacodepoint.array.multidimensional;

public class SymmetricMatrix {

public static boolean isSymmetric(int[][] matrix) {
int rows = matrix.length;
int cols = matrix.length;

// Check if the matrix is square
if (rows != cols) {
return false;
}

// Compare each element with its transposed element
for (int i = 0; i < rows; i++) {
for (int j = i + 1; j < cols; j++) {
if (matrix[i][j] != matrix[j][i]) {
return false;
}
}
}
return true;
}

public static void main(String[] args) {
int[][] matrix1 = { { 1, 2, 3 }, { 2, 4, 5 }, { 3, 5, 6 } };

int[][] matrix2 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

System.out.println("Matrix:");
for (int[] row : matrix1) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
System.out.println("Is Symmetric Matrix: "+isSymmetric(matrix1));

System.out.println("Matrix:");
for (int[] row : matrix2) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
System.out.println("Is Symmetric Matrix: "+isSymmetric(matrix2));
}
}

```

OUTPUT:

Matrix:
1 2 3
2 4 5
3 5 6
Is Symmetric Matrix: true
Matrix:
1 2 3
4 5 6
7 8 9
Is Symmetric Matrix: false

Explanation:

In this program, the static method `isSymmetric`, first checks if the matrix is square (number of rows equals the number of columns). Then, it compares each element with its corresponding transposed element, starting from the top-right half of the matrix. If any element is found to be different, the matrix is not symmetric.

Learn also: Identity Matrix Program.