Most of the java programs can easily be solved or atleast initiated by finding a pattern.
So whats the pattern here?
The pattern is that the relation between array index and the element at that place.
Every index which has 0 in it has 1 so arr[0][0] is 1.See the pattern there?
Every index which has [1] in it has 2. So arr[0][1], arr[1][0] and arr[1][1] are 2 and same is the condition with 3.
The size of the array is also defined in the same way. 3, will be [3][3] matrix.
Hope that helps. 🙂
You are looking for something like this
int m =//input;
int[][] arr = new int[m][m];
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(i>=j)
arr[i][j]=i;
else
arr[i][j]=j;
System.out.print(arr[i][j]);
}
System.out.println();
}
1
solved Java how to return a function that returns a 2d array right angle pattern