A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Java program to check whether a matrix is symmetric or not
Q:

Java program to check whether a matrix is symmetric or not

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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....

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now