forked from utkarsh-shekhar/basic-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricMatrix.java
More file actions
71 lines (67 loc) · 2 KB
/
SymmetricMatrix.java
File metadata and controls
71 lines (67 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Program checks if Matrix is symmetric or not and finds the sum of left and right diagonals
* Accept size M
* Input values in array
* Display original matrix
* Check if marix is symmetric
* Add left diagonal elements and right diagonal elements
* Display if the matrix is symmetric or not
* Print sum of left and right diagonal elements
*/
import java.util.*;
class SymmetricMatrix
{
void main()
{
int M;
int arr[][];
Scanner sc=new Scanner(System.in);
System.out.println("Enter number of rows and coloumn");
M=sc.nextInt();
arr=new int[M][M];
if( M<=0)
{
System.out.println("Enter positive number");
}
else
{
int lsum=0; int rsum=0;
for(int i=0; i<M; i++)
{
for(int j=0; j<M; j++)
{
System.out.println("enter integer");
arr[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(int i=0; i<M; i++)
{//Printing original matrix
for(int j=0; j<M; j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
int c=0;
for(int i=0; i<M; i++)
{
for(int j=0; j<M; j++)
{
if(arr[i][j]!=arr[j][i])
c++;
if(i==j)
lsum +=arr[i][j];
if((i+j)==(M-1))
rsum += arr[i][j];
}
}
if(c==0)
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
System.out.println("The sum of the left diagonal = "+lsum);
System.out.println("The sum of the right diagonal = "+ rsum);
}
}
}