symmetric matrix program in java

Solutions on MaxInterview for symmetric matrix program in java by the best coders in the world

showing results for - "symmetric matrix program in java"
Pietro
07 Aug 2017
1// symmetric matrix program in java
2import java.util.Scanner;
3public class SymmetricMatrixDemo
4{
5   public static void main(String[] args)
6   {
7      Scanner sc = new Scanner(System.in);
8      System.out.println("Please enter number of rows - ");
9      int row = sc.nextInt();
10      System.out.println("Please enter number of columns - ");
11      int col = sc.nextInt();
12      int symMatrix[][] = new int[row][col];
13      System.out.println("Please enter the elements - ");
14      for(int x = 0; x < row; x++)
15      {
16         for(int y = 0; y < col; y++)
17         {
18            symMatrix[x][y] = sc.nextInt();
19         }
20      }
21      System.out.println("Now printing the input matrix - ");
22      for(int x = 0; x < row; x++)
23      {
24         for(int y = 0; y < col; y++)
25         {
26            System.out.print(symMatrix[x][y] + "\t");
27         }
28         System.out.println();
29      }
30      // check if a matrix is symmetric
31      if(row != col)
32      {
33         System.out.println("It's not a square matrix!!");
34      }
35      else
36      {
37         boolean symmetricMatrix = true;
38         for(int x = 0; x < row; x++)
39         {
40            for(int y = 0; y < col; y++)
41            {
42               if(symMatrix[x][y] != symMatrix[y][x])
43               {
44                  symmetricMatrix = false;
45                  break;
46               }
47            }
48         }
49         if(symmetricMatrix)
50         {
51            System.out.println("It's a symmetric matrix!!");
52         }
53         else
54         {
55            System.out.println("It's not a symmetric matrix!!");
56         }
57      }
58      sc.close();
59   }
60}