Given a square matrix of custom size the task is to check whether it is a Symmetric Matrix or not.
Symmetric Matrix is a square matrix which is equal to its transpose.
Example: Consider a matrix:-
1 | 2 | 6 |
2 | 8 | 4 |
6 | 4 | 5 |
It’s transpose is:-
1 | 2 | 6 |
2 | 8 | 4 |
6 | 4 | 5 |
Hence it is a Symmetric Matrix.
Java
import java.util.*;
public class SymmetricMatrix
{
public static void main(String args[])
{
int size=0,flag=1;
Scanner sc=new Scanner(System.in);
System.out.print("Enter size of matrix: ");
size=sc.nextInt();
int ar[][]=new int[size][size];
int tr[][]=new int[size][size];
System.out.println("Enter matrix elements:-");
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
ar[i][j]=sc.nextInt();
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
tr[j][i]=ar[i][j];
}
}
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(ar[i][j]!=tr[i][j])
{
flag=0;
}
}
}
if(flag==1)
{
System.out.println("It is a Symmetric Matrix");
}
else
{
System.out.println("It is not a Symmetric Matrix");
}
}
}
Java