# To check whether a given matrix is Symmetric or not in Java

Hello everyone! here we will learn about how to check whether a given matrix is symmetric or not in Java. In order to check for the matrix to be symmetric, it should satisfy two conditions.

1. It should be a square matrix.

2. Its transpose should retain the original matrix.

Also read;

## Check whether a matrix is symmetric or not!

import java.util.*; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); int r,c; System.out.println("enter no of rows and columns"); r=s.nextInt(); c=s.nextInt(); int[][] a=new int[r][c]; int[][] b=new int[r][c]; System.out.println("enter the matrix elements"); for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { a[i][j]=s.nextInt(); } } if(r==c) { for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { b[i][j]=a[j][i]; } } } int x=0; for(int i=0;i<r;i++) for(int j=0;j<c;j++) if(a[i][j]==b[i][j]) x=1; if(x==1) System.out.println("yes"); else System.out.println("No"); } }

enter no of rows and columns 3 3 enter the matrix elements 1 2 3 2 1 4 3 4 3 yes

From the above example, first, we will read the no of rows and no of columns of the matrix. And then read the elements of the matrix. Now find the transpose of the given matrix. In order to find the transpose of a matrix, first declare an array of the same size to that of the given matrix. Then store the values at their reverse indices. And then compare the actual array and the transpose, if both the matrices are the same then the matrix is symmetric. Note that the matrix declared is a square matrix.

I hope the above example with an explanation is clear and easy to understand how to check if a matrix is Symmetric or not in Java. And you can read the matrix values dynamically by using a Scanner object with no confusion.

## Leave a Reply