Symmetric Matrix Program In Java

Welcome to flower brackets blog. Today we are going to learn symmetric matrix program in java.

Now obviously for a matrix to be symmetric, first it should be a square matrix and every element at ith row and jth column should be equal to every element at jth row and ith column.

For example:

Symmetric Matrix Program In Java

In other words if A[x][y] is a square matrix with (3 X 3) order, then A[x][y] matrix is said to be symmetric if every element at xth row and yth column is equal to element at yth row and xth column.

That is A[x][y] == A[y][x]

example: Symmetric Matrix Program In Java

Let’s see an example on symmetric matrix in java

Also Read – How To Create Matrix In Java

import java.util.Scanner;

public class SymmetricMatrixDemo
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter number of rows - ");
int row = sc.nextInt();

System.out.println("Please enter number of columns - ");
int col = sc.nextInt();

int symMatrix[][] = new int[row][col]; System.out.println("Please enter the elements - ");

for(int x = 0; x < row; x++)
{
for(int y = 0; y < col; y++)
{
symMatrix[x][y] = sc.nextInt();
}
}

System.out.println("Now printing the input matrix - ");
for(int x = 0; x < row; x++)
{
for(int y = 0; y < col; y++)
{
System.out.print(symMatrix[x][y] + "\t");
}
System.out.println();
}

// Now checking for symmetric matrix

if(row != col)
{
System.out.println("It's not a square matrix!!");
}
else
{
boolean symmetricMatrix = true;
for(int x = 0; x < row; x++)
{
for(int y = 0; y < col; y++)
{
if(symMatrix[x][y] != symMatrix[y][x])
{
symmetricMatrix = false;
break;
}
}
}

if(symmetricMatrix)
{
System.out.println("It's a symmetric matrix!!");
}
else
{
System.out.println("It's not a symmetric matrix!!");
}
}
sc.close();
}
}

Output:

Symmetric Matrix Program In Java


conclusion

So that’s it guys. This is all about how to execute symmetric matrix program in java. I hope you have understood the concept of matrix.

You can subscribe to my blog flower brackets if you haven’t.

If you like this post you can share it across social networks.

About the author

admin

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *