-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalSums.java
More file actions
34 lines (31 loc) · 1.15 KB
/
Copy pathDiagonalSums.java
File metadata and controls
34 lines (31 loc) · 1.15 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
import java.io.*;
public class DiagonalSums {
public static void main() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER THE NUMBER OF ROWS or COLUMN =");
int n = Integer.parseInt(br.readLine());
int arr[][] = new int[n][n];
System.out.println("ENTER THE ELEMENTS=");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = Integer.parseInt(br.readLine());
}
}
System.out.println("MATRIX IS=");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
int leftDiagonalSum = 0, rightDiagonalSum = 0;
for (int i = 0; i < n; i++) {
leftDiagonalSum += arr[i][i];
}
for (int j = 0; j < n; j++) {
rightDiagonalSum += arr[j][n - 1 - j];
}
System.out.println("SUM OF LEFT DIAGONAL=" + leftDiagonalSum);
System.out.println("SUM OF RIGHT DIAGONAL=" + rightDiagonalSum);
}
}