-
Notifications
You must be signed in to change notification settings - Fork 53
/
PrintMatrixDiagonal.java
43 lines (38 loc) · 1.37 KB
/
PrintMatrixDiagonal.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
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.lang.*;
import java.util.*;
public class PrintDiagonalElements {
public static void main(String[] args) {
// Taking input for the matrix dimensions
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // number of rows
int m = sc.nextInt(); // number of columns
int[][] C = new int[n][m];
// Taking input for the matrix elements
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
C[i][j] = sc.nextInt();
}
}
// Printing diagonals starting from the first row
for (int col = 0; col < m; col++) {
int r = 0; // Start from the first row
int c = col; // Current column
while (r < n && c >= 0) {
System.out.print(C[r][c] + " ");
r++; // Move down
c--; // Move left
}
}
// Printing diagonals starting from the second row of the first column
for (int row = 1; row < n; row++) {
int r = row; // Current row
int c = m - 1; // Start from the last column
while (r < n && c >= 0) {
System.out.print(C[r][c] + " ");
r++; // Move down
c--; // Move left
}
}
System.out.println(); // For a newline after printing all diagonals
}
}