Write a Java Program to find Matrix is a Symmetric Matrix with an example. A Symmetric Matrix is a square matrix, and if it is equal to its transposed Matrix.
Java Program to find Matrix is a Symmetric Matrix
In this Java Symmetric Matrix example, we declared an org_arr 3 * 3 integer matrix. Next, we used another one, trans_arr, to store the Transposed Matrix. Within the other for loop, we used the If statement to check whether the elements in this org_arr are not equal to the transposed trans_arr Matrix. If true, we incremented the count value and applied the break statement to exit from the loop. Next, we used the If Else statement to print the Symmetric matrix output based on the count value.
public class SymmetricMatrix { public static void main(String[] args) { int i, j, count = 1; int[][] org_arr = {{15, 25, 35}, {45, 55, 65}, {75, 85, 95}}; int[][] trans_arr = new int[3][3]; for(i = 0; i < org_arr.length ; i++) { for(j = 0; j < org_arr[0].length; j++) { trans_arr[j][i] = org_arr[i][j]; } } System.out.println("\nAfter Transposing Matrix Items are :"); for(i = 0; i < trans_arr.length ; i++) { for(j = 0; j < trans_arr[0].length; j++) { System.out.format("%d \t", trans_arr[i][j]); } System.out.print("\n"); } for(i = 0; i < org_arr.length ; i++) { for(j = 0; j < org_arr[0].length; j++) { if(org_arr[i][j] != trans_arr[i][j]) { count++; break; } } } if(count == 1) { System.out.println("\nMatrix is a Symmetric Matrix"); } else { System.out.println("\nMatrix is Not a Symmetric Matrix"); } } }
Java Program to find Matrix is a Symmetric Matrix Example 2
This Java code is the same as the above. However, this Symmetric code allows the user to enter the number of rows, columns, and matrix items.
import java.util.Scanner; public class Example { private static Scanner sc; public static void main(String[] args) { int i, j, rows, columns, count = 1; sc= new Scanner(System.in); System.out.println("\n Enter Rows and Columns : "); rows = sc.nextInt(); columns = sc.nextInt(); int[][] org_arr = new int[rows][columns]; int[][] trans_arr = new int[rows][columns]; System.out.println("\n Please Enter the Sparse Matrix Items : "); for(i = 0; i < rows; i++) { for(j = 0; j < columns; j++) { org_arr[i][j] = sc.nextInt(); } } for(i = 0; i < org_arr.length ; i++) { for(j = 0; j < org_arr[0].length; j++) { trans_arr[j][i] = org_arr[i][j]; } } System.out.println("\nAfter Transposing Matrix Items are :"); for(i = 0; i < trans_arr.length ; i++) { for(j = 0; j < trans_arr[0].length; j++) { System.out.format("%d \t", trans_arr[i][j]); } System.out.print("\n"); } for(i = 0; i < org_arr.length ; i++) { for(j = 0; j < org_arr[0].length; j++) { if(org_arr[i][j] != trans_arr[i][j]) { count++; break; } } } if(count == 1) { System.out.println("\nIt is a Symmetric Matrix"); } else { System.out.println("\nIt is Not"); } } }
Symmetric Matrix output
Enter Rows and Columns :
3 3
Please Enter the Sparse Matrix Items :
1 2 3
2 4 5
3 5 4
After Transposing Matrix Items are :
1 2 3
2 4 5
3 5 4
It is a Symmetric Matrix
Let me try with other values to check for Symmetric Matrix.
Enter Rows and Columns :
3 3
Please Enter the Sparse Matrix Items :
1 2 3
4 5 6
7 8 9
After Transposing Matrix Items are :
1 4 7
2 5 8
3 6 9
It is Not