Print Spiral Pattern in C / C++ / Java / Python / C#

Spiral pattern

Programs to print the spiral pattern using numbers are given here. If the size of this pattern is s, the pattern is completed after printing $s^2$.






1. Program & output to print the spiral pattern




1.1. C Program & output to print the spiral pattern

Code has been copied
/**********************
  alphabetacoder.com
C program for a spiral 
pattern using numbers
***********************/

#include <stdio.h>

#define SIZE 10

int main() {
    // declare variables
    int pattern[SIZE][SIZE] = {0};
    int i, j, m, n, p, q, s, num;

    // take input 
    printf("Enter the size of the pattern: ");
    scanf("%d", & s);

    // initialize
    num = 1; // starting number
    i = 0; // 2d array row index
    j = 0; // 2d array column index
    m = 0; // to store row index lower limit
    n = s - 1; // to store row index upper limit
    p = 0; // to store column index lower limit
    q = s - 1; // to store column index upper limit

    // fill the array to create patern.
    // stop when n > s * s
    while (num <= s * s) {
        // fill horizontally left to right
        for (j = p; j <= q; j++) {
            pattern[m][j] = num;
            num++;
        }

        // update row index lower limit
        m++;

        // fill vertically top to bottom
        for (i = m; i <= n; i++) {
            pattern[i][q] = num;
            num++;
        }

        // update column index upper limit
        q--;

        // fill horizontally right to left
        for (j = q; j >= p; j--) {
            pattern[n][j] = num;
            num++;
        }

        // update row index upper limit
        n--;

        // fill vertically bottom to top
        for (i = n; i >= m; i--) {
            pattern[i][p] = num;
            num++;
        }

        // update column index lower limit
        p++;
    }

    // new line
    printf("\n");

    // display the pattern
    for (i = 0; i < s; i++) {
        for (j = 0; j < s; j++) {
            // print value
            printf("%d\t", pattern[i][j]);
        }
        // new line
        printf("\n");
    }

    return 0;
}

Output


Case 1:

Enter the size of the pattern: 5


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


Case 2:

Enter the size of the pattern: 10


1  2  3  4  5  6  7  8  9  10

36 37 38 39 40 41 42 43 44 11

35 64 65 66 67 68 69 70 45 12

34 63 84 85 86 87 88 71 46 13

33 62 83 96 97 98 89 72 47 14

32 61 82 95 100 99 90 73 48 15

31 60 81 94 93 92 91 74 49 16

30 59 80 79 78 77 76 75 50 17

29 58 57 56 55 54 53 52 51 18

28 27 26 25 24 23 22 21 20 19




1.2. C++ Program & output to print the spiral pattern

Code has been copied
/**********************
  alphabetacoder.com
C++ program for a spiral 
pattern using numbers
***********************/

#include <iostream>
#define SIZE 10

using namespace std;

int main() {
    // declare variables
    int pattern[SIZE][SIZE] = {0};
    int i, j, m, n, p, q, s, num;

    // take input 
    cout << "Enter the size of the pattern: ";
    cin >> s;

    // initialize
    num = 1; // starting number
    i = 0; // 2d array row index
    j = 0; // 2d array column index
    m = 0; // to store row index lower limit
    n = s - 1; // to store row index upper limit
    p = 0; // to store column index lower limit
    q = s - 1; // to store column index upper limit

    // fill the array to create patern.
    // stop when n > s * s
    while (num <= s * s) {
        // fill horizontally left to right
        for (j = p; j <= q; j++) {
            pattern[m][j] = num;
            num++;
        }

        // update row index lower limit
        m++;

        // fill vertically top to bottom
        for (i = m; i <= n; i++) {
            pattern[i][q] = num;
            num++;
        }

        // update column index upper limit
        q--;

        // fill horizontally right to left
        for (j = q; j >= p; j--) {
            pattern[n][j] = num;
            num++;
        }

        // update row index upper limit
        n--;

        // fill vertically bottom to top
        for (i = n; i >= m; i--) {
            pattern[i][p] = num;
            num++;
        }

        // update column index lower limit
        p++;
    }

    // new line
    cout << endl;

    // display the pattern
    for (i = 0; i < s; i++) {
        for (j = 0; j < s; j++) {
            // print value
            cout << pattern[i][j] << "\t";
        }
        // new line
        cout << endl;
    }

    return 0;
}

Output


Enter the size of the pattern: 6


1  2  3  4  5  6

20 21 22 23 24 7

19 32 33 34 25 8

18 31 36 35 26 9

17 30 29 28 27 10

16 15 14 13 12 11





1.3. Java Program & output to print the spiral pattern

Code has been copied
/*************************
  alphabetacoder.com
Java program for a spiral 
pattern using numbers
**************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);
        // declare variables
        int i, j, m, n, p, q, s, num;

        // take input 
        System.out.print("Enter the size of the pattern: ");
        s = sc.nextInt();

        // declare 2D array
        int[][] pattern = new int[s][s];

        // initialize
        num = 1; // starting number
        i = 0; // 2d array row index
        j = 0; // 2d array column index
        m = 0; // to store row index lower limit
        n = s - 1; // to store row index upper limit
        p = 0; // to store column index lower limit
        q = s - 1; // to store column index upper limit

        // fill the array to create patern.
        // stop when n > s * s
        while (num <= s * s) {
            // fill horizontally left to right
            for (j = p; j <= q; j++) {
                pattern[m][j] = num;
                num++;
            }

            // update row index lower limit
            m++;

            // fill vertically top to bottom
            for (i = m; i <= n; i++) {
                pattern[i][q] = num;
                num++;
            }

            // update column index upper limit
            q--;

            // fill horizontally right to left
            for (j = q; j >= p; j--) {
                pattern[n][j] = num;
                num++;
            }

            // update row index upper limit
            n--;

            // fill vertically bottom to top
            for (i = n; i >= m; i--) {
                pattern[i][p] = num;
                num++;
            }

            // update column index lower limit
            p++;
        }

        // new line
        System.out.println("");

        // display the pattern
        for (i = 0; i < s; i++) {
            for (j = 0; j < s; j++) {
                // print value
                System.out.print(pattern[i][j] + "\t");
            }
            // new line
            System.out.println("");
        }
    }
}

Output


Enter the size of the pattern: 4


1  2  3  4 

12 13 14 5 

11 16 15 6 

10 9  8  7 





1.4. Python Program & output to print the spiral pattern

Code has been copied
#****************************
#  alphabetacoder.com
#Python program for a spiral 
#pattern using numbers
#****************************

# take input 
s = int(input("Enter the size of the pattern: "))

# declare 2D array
pattern = [[0 for i in range(s)] for j in range(s)]

# initialize
num = 1 # starting number
i = 0 # 2d array row index
j = 0 # 2d array column index
m = 0 # to store row index lower limit
n = s - 1 # to store row index upper limit
p = 0 # to store column index lower limit
q = s - 1 # to store column index upper limit


# fill the array to create patern.
# stop when n > s * s
while num <= s * s:
    # fill horizontally left to right
    for j in range(p, q + 1):
        pattern[m][j] = num
        num = num + 1

    # update row index lower limit
    m = m + 1

    # fill vertically top to bottom
    for i in range(m, n + 1):
        pattern[i][q] = num
        num = num + 1

    # update column index upper limit
    q = q - 1

    # fill horizontally right to left
    for j in range(q, p - 1, -1):
        pattern[n][j] = num
        num = num + 1

    # update row index upper limit
    n = n - 1

    # fill vertically bottom to top
    for i in range(n, m - 1, -1):
        pattern[i][p] = num
        num = num + 1

    # update column index lower limit
    p = p + 1

# new line
print("");

# display the pattern
for i in range(0, s):
    for j in range(0, s):
        # print value
        print(pattern[i][j], end = "\t")
        
    # new line
    print("")

Output


Enter the size of the pattern: 7


1  2  3  4  5  6  7 

24 25 26 27 28 29 8 

23 40 41 42 43 30 9 

22 39 48 49 44 31 10 

21 38 47 46 45 32 11 

20 37 36 35 34 33 12 

19 18 17 16 15 14 13 





1.5. C# Program & output to print the spiral pattern

Code has been copied
/**********************
  alphabetacoder.com
C# program for a spiral 
pattern using numbers
***********************/

using System;

namespace Spiral {
    class Program {
        static void Main(string[] args) {
            // declare variables
            int i, j, m, n, p, q, s, num;

            // take input
            Console.Write("Enter the size of the pattern: ");
            s = Convert.ToInt32(Console.ReadLine());
            
            // declare a 2D array variables
            int[, ] pattern = new int[s, s];

            // initialize
            num = 1; // starting number
            i = 0; // 2d array row index
            j = 0; // 2d array column index
            m = 0; // to store row index lower limit
            n = s - 1; // to store row index upper limit
            p = 0; // to store column index lower limit
            q = s - 1; // to store column index upper limit

            // fill the array to create patern.
            // stop when n > s * s
            while (num <= s * s) {
                // fill horizontally left to right
                for (j = p; j <= q; j++) {
                    pattern[m, j] = num;
                    num++;
                }

                // update row index lower limit
                m++;

                // fill vertically top to bottom
                for (i = m; i <= n; i++) {
                    pattern[i, q] = num;
                    num++;
                }

                // update column index upper limit
                q--;

                // fill horizontally right to left
                for (j = q; j >= p; j--) {
                    pattern[n, j] = num;
                    num++;
                }

                // update row index upper limit
                n--;

                // fill vertically bottom to top
                for (i = n; i >= m; i--) {
                    pattern[i, p] = num;
                    num++;
                }

                // update column index lower limit
                p++;
            }

            // new line
            Console.WriteLine("");

            // display the pattern
            for (i = 0; i < s; i++) {
                for (j = 0; j < s; j++) {
                    // print value
                    Console.Write("\t" + pattern[i, j]);
                }
                // new line
                Console.WriteLine("");
            }
            // wait for user to press any key
            Console.ReadKey();
        }
    }
}

Output


Enter the size of the pattern: 9


1  2  3  4  5  6  7  8  9 

32 33 34 35 36 37 38 39 10 

31 56 57 58 59 60 61 40 11 

30 55 72 73 74 75 62 41 12 

29 54 71 80 81 76 63 42 13 

28 53 70 79 78 77 64 43 14 

27 52 69 68 67 66 65 44 15 

26 51 50 49 48 47 46 45 16 

25 24 23 22 21 20 19 18 17