Java program to check whether a matrix is symmetric or not

In this java program, we are reading a matrix and check whether input matrix is a symmetric matrix or not? By IncludeHelp Last updated : December 23, 2023

Problem statement

Given a matrix and we have to check whether it is symmetric or not using Java program?

Symmetric Matrix

If the sum of the left diagonal and right diagonal of the matrix is equal then the above matrix is said to be symmetric matrix. The entries of a symmetric matrix are symmetric with respect to the main diagonal.

Example

Enter the number of elements : 3

Input the elements of the Matrix :
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11

The Original Matrix is : 
11	11	11	
11	11	11	
11	11	11	

The given Matrix is Symmetric
The sum of the left diagonal = 33
The sum of the right diagonal = 33

Program to check matrix is symmetric or not in java

// This program will find weather 
// the matrix is symmetric or not.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExArrayFindSymmetric {
  public static void main(String args[]) throws IOException {
    // create buffer class object.
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    // enter how many elements you have to enter.
    System.out.print("Enter the number of elements : ");
    int m = Integer.parseInt(br.readLine());
    int A[][] = new int[m][m];

    // Check for rows and column.
    if (m > 2 && m < 10) {
      // enter the elements here.
      System.out.println("\nInput the elements of the Matrix : \n");
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          System.out.print("Enter the elements : ");
          A[i][j] = Integer.parseInt(br.readLine());
        }
      }

      // here the original matrix.
      System.out.println("\nThe Original Matrix is : ");
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          System.out.print(A[i][j] + "\t");
        }
        System.out.println();
      }

      // check weather matrix is symmetric or not.
      int flag = 0;
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          if (A[i][j] != A[j][i]) {
            // When elements do not matched.
            flag = 1;
            break;
          }
        }
      }
      // check for symmetric or not.
      if (flag == 1)
        System.out.println("\nThe given Matrix is Not Symmetric");
      else
        System.out.println("\nThe given Matrix is Symmetric");

      // calculate sum of diagonals.
      int ld = 0, rd = 0;
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          // for the left diagonal
          if (i == j) {
            ld = ld + A[i][j];
          }
          // for the right diagonal
          if ((i + j) == (m - 1)) {
            rd = rd + A[i][j];
          }
        }
      }

      // print sum of left and right diagonals.
      System.out.println("The sum of the left diagonal = " + ld);
      System.out.println("The sum of the right diagonal = " + rd);
    } else
      System.out.println("The Matrix Size is Out Of Range....");
  }
}

Output

First run:
Enter the number of elements : 3

Input the elements of the Matrix :
Enter the elements : 11
Enter the elements : 12
Enter the elements : 12
Enter the elements : 55
Enter the elements : 65
Enter the elements : 66
Enter the elements : 45
Enter the elements : 25
Enter the elements : 35

The Original Matrix is : 
11	12	12	
55	65	66	
45	25	35	

The given Matrix is Not Symmetric
The sum of the left diagonal = 111
The sum of the right diagonal = 122

Second run:
Enter the number of elements : 3

Input the elements of the Matrix :
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11

The Original Matrix is : 
11	11	11	
11	11	11	
11	11	11	

The given Matrix is Symmetric
The sum of the left diagonal = 33
The sum of the right diagonal = 33

Third run:
Enter the number of elements : 2
The Matrix Size is Out Of Range....

Java Array Programs »

More Java Array Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.