
write a program to print pattern shown below
1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9

Here is an example of number spiral.
public class NumberSpiral{
public static void main(String[] args) {
int dimension=5;
int[][] spiralArray = new int[dimension][dimension];
int num = (int) Math.ceil((dimension) / 2.0);
int j;
int len = dimension;
int number = 1;
for (int i = 0; i < num; i++) {
for (j = 0; j < len; j++) {
spiralArray[i][i + j] = number++;
}
for (j = 1; j < len; j++) {
spiralArray[i + j][dimension - 1 - i] = number++;
}
for (j = len - 2; j > -1; j--) {
spiralArray[dimension - 1 - i][i + j] = number++;
}
for (j = len - 2; j > 0; j--) {
spiralArray[i + j][i] = number++;
}
len -= 2;
}
for (int[] row : spiralArray) {
for (int elem : row) {
System.out.printf("%3d", elem);
}
System.out.println();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.