I wanted to print a 2D array matrix with a pattern (right half pyramid) and each position will store a number that will increase as the position change.
I created the pattern using a 2D array and assigned an integer value to each position which should increase as the position changes but some positions have a 0 value.
public class Main {
public static void main(String[] args) {
int[][] twoD = new int[4][4];
int i, j, k = 0;
for (i = 0; i < 4; i++){
for (j = 0; j < i + 1; j++) {
twoD[i][i] = k;
k++;
System.out.print(twoD[i][j] + " ");
}
System.out.println();
}
}
Output :-
0
0 2
0 0 5
0 0 0 9
Expected Output:-
0
1 2
3 4 5
6 7 8 9
Check the indexing on your 2D array. I believe you typed i
when you meant to type j
.