Question
Program to check whether the matrix is symmetric matrix
(symmetric matrix is a square matrix that is equal to its transpose)
Matrix:
[3, -2, 4]
[-2, 6, 2]
[4, 2, 3 ]
TRANSPOSE:
[3, -2, 4]
[-2, 6, 2]
[4, 2, 3]
THE MATRIX IS SYMMETRIC MATRIX
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class symmetricMatrix
{
public static void main(String[] args)
{
int row=0, col=0,i=0,j=0;
int a[][],t[][];
boolean flag=true;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE ROW OF MATRIX");
row=sc.nextInt();
System.out.println("ENTER THE COLUMN OF MATRIX");
col=sc.nextInt();
if(row!=col)
{
System.out.println("MATRIX NEEDS TO BE SQUARE MATRIX");
}
else
{
a=new int[row][col];
t=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
t[i][j]=a[j][i];
/*checking if element of transpose matrix is not equal to element original matrix at positions 'i' and 'j'*/
if(t[i][j]!=a[i][j])
{
flag=false;
}
}
}
System.out.println("TRANSPOSE OF MATRIX: ");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(t[i][j]+" ");
}
System.out.println();
}
if(flag==false)
{
System.out.println("MATRIX IS NOT SYMMETRIC MATRIX");
}
else
{
System.out.println("MATRIX IS SYMMETRIC MATRIX");
}
}
}
}