Advertisement
Didart

Diagonal Difference

Jan 14th, 2023
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. package MultidimensionalArrays2;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DiagonalDifference {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int matrixSize = Integer.parseInt(scanner.nextLine());
  10.  
  11.         int[][] matrix = new int[matrixSize][matrixSize];
  12.  
  13.         int sum = 0; int sum2 = 0;
  14.         for (int row = 0; row < matrixSize; row++) {
  15.             String[] line = scanner.nextLine().split(" ");
  16.             for (int col = 0; col < matrixSize; col++) {
  17.                 matrix[row][col] = Integer.parseInt(line[col]);
  18.             }
  19.         }
  20.         for (int col = 0; col < matrixSize; col++) {
  21.             sum = matrix[col][col] + sum;
  22.         }
  23.         for (int col = 0; col < matrixSize; col++) {
  24.             sum2 = matrix[col][matrixSize-1-col] + sum2;
  25.         }
  26.         if (sum>sum2){
  27.             System.out.println(sum-sum2);
  28.         }else {
  29.             System.out.println(sum2-sum);
  30.         }
  31.     }
  32. }
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement