Spiral Pattern

Given a number N, the task is to print the following pattern:-

Examples:

Input : N = 4
Output : 4 4 4 4 4 4 4
         4 3 3 3 3 3 4
         4 3 2 2 2 3 4
         4 3 2 1 2 3 4
         4 3 2 2 2 3 4
         4 3 3 3 3 3 4
         4 4 4 4 4 4 4

Input : N = 2
Output : 2 2 2
         2 1 2
         2 2 2

Approach 1: The common observation is that the square thus formed will be of size (2*N-1)x(2*N-1). Fill the first row and column, last row and column with N, and then gradually decrease N and fill the remaining rows and columns similarly. Decrease N every time after filling 2 rows and 2 columns.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the
// spiral pattern
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the pattern
void pattern(int value)
{
    // Declare a square matrix
    int row = 2 * value - 1;
    int column = 2 * value - 1;
    int arr[row][column];
  
    int i, j, k;
  
    for (k = 0; k < value; k++) {
  
        // store the first row
        // from 1st column to last column
        j = k;
        while (j < column - k) {
            arr[k][j] = value - k;
            j++;
        }
  
        // store the last column
        // from top to bottom
        i = k + 1;
        while (i < row - k) {
            arr[i][row - 1 - k] = value - k;
            i++;
        }
  
        // store the last row
        // from last column to 1st column
        j = column - k - 2;
        while (j >= k) {
            arr[column - k - 1][j] = value - k;
            j--;
        }
  
        // store the first column
        // from bottom to top
        i = row - k - 2;
        while (i > k) {
            arr[i][k] = value - k;
            i--;
        }
    }
  
    // print the pattern
    for (i = 0; i < row; i++) {
        for (j = 0; j < column; j++) {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int n = 5;
    pattern(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print
// the spiral pattern
class GFG {
  
    // Function to print the pattern
    static void pattern(int value)
    {
        // Declare a square matrix
        int row = 2 * value - 1;
        int column = 2 * value - 1;
        int[][] arr = new int[row][column];
  
        int i, j, k;
  
        for (k = 0; k < value; k++) {
  
            // store the first row
            // from 1st column to last column
            j = k;
            while (j < column - k) {
                arr[k][j] = value - k;
                j++;
            }
  
            // store the last column
            // from top to bottom
            i = k + 1;
            while (i < row - k) {
                arr[i][row - 1 - k] = value - k;
                i++;
            }
  
            // store the last row
            // from last column
            // to 1st column
            j = column - k - 2;
            while (j >= k) {
                arr[column - k - 1][j] = value - k;
                j--;
            }
  
            // store the first column
            // from bottom to top
            i = row - k - 2;
            while (i > k) {
                arr[i][k] = value - k;
                i--;
            }
        }
  
        // print the pattern
        for (i = 0; i < row; i++) {
            for (j = 0; j < column; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
        pattern(n);
    }
}
  
// This code is contributed
// by ChitraNayal

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to print 
# the spiral pattern
  
# Function to print the pattern
def pattern(value):
      
    # Declare a square matrix
    row = 2 * value - 1
    column = 2 * value - 1
    arr = [[0 for i in range(row)] 
              for j in range (column)]
  
    for k in range( value):
  
        # store the first row
        # from 1st column to 
        # last column
        j = k
        while (j < column - k):
            arr[k][j] = value - k
            j += 1
  
        # store the last column
        # from top to bottom
        i = k + 1
        while (i < row - k):
            arr[i][row - 1 - k] = value - k
            i += 1
  
        # store the last row
        # from last column 
        # to 1st column
        j = column - k - 2
        while j >= k :
            arr[column - k - 1][j] = value - k
            j -= 1
  
        # store the first column
        # from bottom to top
        i = row - k - 2
        while i > k :
            arr[i][k] = value - k
            i -= 1
  
    # print the pattern
    for i in range(row):
        for j in range(column):
            print(arr[i][j], end = " ")
        print()
      
# Driver code
if __name__ == "__main__":
    n = 5
    pattern(n)
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print
// the spiral pattern
using System;
  
class GFG {
  
    // Function to print the pattern
    static void pattern(int value)
    {
  
        // Declare a square matrix
        int row = 2 * value - 1;
        int column = 2 * value - 1;
        int[, ] arr = new int[row, column];
  
        int i, j, k;
  
        for (k = 0; k < value; k++) {
  
            // store the first row
            // from 1st column to
            // last column
            j = k;
            while (j < column - k) {
                arr[k, j] = value - k;
                j++;
            }
  
            // store the last column
            // from top to bottom
            i = k + 1;
            while (i < row - k) {
                arr[i, row - 1 - k] = value - k;
                i++;
            }
  
            // store the last row
            // from last column
            // to 1st column
            j = column - k - 2;
            while (j >= k) {
                arr[column - k - 1, j] = value - k;
                j--;
            }
  
            // store the first column
            // from bottom to top
            i = row - k - 2;
            while (i > k) {
                arr[i, k] = value - k;
                i--;
            }
        }
  
        // print the pattern
        for (i = 0; i < row; i++) {
            for (j = 0; j < column; j++) {
                Console.Write(arr[i, j] + " ");
            }
            Console.Write("\n");
        }
    }
  
    // Driver code
    public static void Main()
    {
        int n = 5;
        pattern(n);
    }
}
  
// This code is contributed
// by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to print 
// the spiral pattern
  
// Function to print the pattern
function pattern($value)
{
    // Declare a square matrix
    $row = 2 * $value - 1;
    $column = 2 * $value - 1;
    $arr = array(array());
  
    for ($k = 0; $k < $value; $k++) 
    {
  
        // store the first row
        // from 1st column to 
        // last column
        $j = $k;
        while ($j < $column - $k)
        {
            $arr[$k][$j] = $value - $k;
            $j++;
        }
  
        // store the last column
        // from top to bottom
        $i = $k + 1;
        while ($i < $row - $k)
        {
            $arr[$i][$row - 1 - $k] = $value - $k;
            $i++;
        }
  
        // store the last row
        // from last column 
        // to 1st column
        $j = $column - $k - 2;
        while ($j >= $k
        {
            $arr[$column - $k - 1][$j] = $value - $k;
            $j--;
        }
  
        // store the first column
        // from bottom to top
        $i = $row - $k - 2;
        while ($i > $k
        {
            $arr[$i][$k] = $value - $k;
            $i--;
        }
    }
  
    // print the pattern
    for ($i = 0; $i < $row; $i++)
    {
        for ($j = 0; $j < $column; $j++)
        {
            echo $arr[$i][$j] . " ";
        }
        echo "\n";
    }
}
  
// Driver code
$n = 5;
pattern($n);
  
// This code is contributed 
// by ChitraNayal
?>

chevron_right


Output:

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5

Approach 2: Starting the indexing from i = 1 and j = 1, it can be observed that every value of the required matrix will be max(abs(i – n), abs(j – n)) + 1.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <algorithm>
#include <iostream>
using namespace std;
  
// Function to print the required pattern
void pattern(int n)
{
  
    // Calculating boundary size
    int p = 2 * n - 1;
  
    for (int i = 1; i <= p; i++) {
        for (int j = 1; j <= p; j++) {
  
            // Printing the values
            cout << max(abs(i - n), abs(j - n)) + 1 << " ";
        }
        cout << endl;
    }
}
  
// Driver code
int main()
{
    int n = 5;
  
    pattern(n);
  
    return 0;
}
// This code is contributed by : Vivek kothari

chevron_right


Output:

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Ita_c, vivekkothari