. Given a number N, create a 2D arrays with n rows and n columns. Now inspect the matrix pattern below and come up with a formula to populate the array for any give NXN matrix.
public class Main {
public static void main(String[] args) {
int n = 5;
int[][] array = new int[n][n];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length - i; j++) {
array[i][j] = 1;
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}
Comments
Leave a comment