Open In App

Java Program to Print Spiral Pattern of Numbers

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, printing a spiral number is a very common programming task, which helps us to understand loops and array manipulation in depth. In this article, we are going to print a spiral pattern of numbers for a given size n.

What is a Spiral Pattern?

A spiral pattern is a sequence of numbers arranged in a matrix, such as the number starts from the top left and moves towards the right, and then down, and then left, and then up, and keeps on doing this until all the cells of the matrix are filled.

Let's understand this with the help of an example.

Example: For n = 4, the spiral pattern looks something like this.

1 2 3 4

12 13 14 5

11 16 15 6

10 9 8 7

How Does the Program Work?

First, the program creates an empty 2D matrix of size n x n. After this, we need to fill the matrix with numbers starting from 1 to n x n, in a spiral way.

Now, the interesting question arises, how are we going to move inside the matrix?

To move inside the matrix, the program follows the four directions, which are listed below:

  • Right
  • Down
  • Left
  • Up

We start moving right from the top-left corner and then going to fill in the numbers one by one, and when we reach the end of the matrix, we turn and change the direction in this order, which is listed below:

Right → Down → Left → Up → Right → ........... and keep on repeating the same

We keep on filling the numbers until all the cells are filled.

Let's now see the implementation of the code for better understanding.

Implementation:

Java
// Demonstrating printing spiral
// pattern of numbers

public class Geeks {

    public static void printSpiral(int size) {

        int row = 0, col = 0;
        int b = size - 1;
        int sl = size - 1;
        int f = 1;
        char m = 'r';

        int[][] matrix = new int[size][size];

        for (int i = 1; i <= size * size; i++) {

            matrix[row][col] = i;

            switch (m) {
                case 'r':
                    col++;
                    break;
                case 'd':
                    row++;
                    break;
                case 'l':
                    col--;
                    break;
                case 'u':
                    row--;
                    break;
            }

            if (i == b) {
                b += sl;

                if (f != 2) {
                    f = 2;
                } else {
                    f = 1;
                    sl--;
                }

                switch (m) {
                    case 'r':
                        m = 'd';
                        break;
                    case 'd':
                        m = 'l';
                        break;
                    case 'l':
                        m = 'u';
                        break;
                    case 'u':
                        m = 'r';
                        break;
                }
            }
        }

        for (row = 0; row < size; row++) {
            for (col = 0; col < size; col++) {
                System.out.print(matrix[row][col] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int size = 4;
        printSpiral(size);
    }
}

Output
1 2 3 4 
12 13 14 5 
11 16 15 6 
10 9 8 7 

Explanation: Here, first we are creating a empty matrix of size size x size, after this we start filling numbers from 1 to size x size. The numbers move right, then down, then left, then up. We have declared two variable row and col which keep track of where we need to put the next number. The variable m is used to tell, which direction we need to move next and the varible b tells when we need to change the direction. The program keeps on filling the numbers and changing the directions until the whole matrix is filled and in the end we printing the sprial pattern.


Next Article
Article Tags :
Practice Tags :

Similar Reads