-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixDiagonals.java
30 lines (28 loc) · 1.09 KB
/
MatrixDiagonals.java
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
package Matrices_Exercises;
import java.util.Scanner;
public class MatrixDiagonals {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
int[][] matrix = new int[n][n];
int diagonal = 0;
int secondaryDiagonal = 0;
for (int row = 0; row < matrix.length; row++) {
String [] nums = scan.nextLine().split(" ");
diagonal += Integer.parseInt(nums[row]);
for (int col = 0; col < matrix.length; col++) {
int currentNum = Integer.parseInt(nums[col]);
matrix[row][col]= currentNum;
}
}
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix.length; col++) {
if(col == matrix.length - row - 1){
int currentNum = matrix[row][col];
secondaryDiagonal += currentNum;
}
}
}
System.out.println(Math.abs(diagonal - secondaryDiagonal));
}
}