# C++ Program to Print Spiral Pattern of Numbers

Displaying numbers in different formats comes under fundamental coding problems to learn different concepts of coding like conditional statements and looping statements. There are different programs where we have printed special characters like stars to make triangles or squares. In this article, we will print numbers in spiral form like a square in C++.

We will take the number of lines n as input, then starting from the top-left corner we will move towards the right, then down, then left and then up, then again towards the right, and so on.

## Spiral Pattern with Numbers

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


To solve this, we will use a 2D matrix of size n x n, In this example, we have taken n = 7. Then fill the matrix from the top-left corner in a spiral fashion. And at the end print the entire matrix. Here, we are printing from 1 to 7 on the first row, then the flow is changing its direction, moving towards the bottom up to 13, then again towards left up till 19, and finally going upward up till 24, then right again, and so on. Let us see the algorithm for a better understanding.

### Algorithm

• Take input s as the number of lines
• create one s x s matrix and initialize them with 0
• num := 1
• initialize i, j, m with 0
• initialize n := s - 1, p := 0 and q := s - 1
• while num is not exceeding s * s, do
• for j ranging from p to q, do
• mat[ m, j ] := num
• num := num + 1
• end for
• m := m + 1
• for i ranging from m to n, do
• mat[ i, q ] := num
• num := num + 1
• end for
• q := q - 1
• for j ranging from q to p, decreasing j by 1, do
• mat[ n, j ] := num
• num := num + 1
• end for
• n := n - 1
• for i ranging from n to m, decrease i by 1, do
• mat[ i, p ] := num
• num := num + 1
• end for
• p := p + 1
• end while
• for i ranging from 0 to s - 1, do
• for j ranging from 0 to s - 1, do
• display mat[ i, j ]
• end for
• move the cursor to the next line
• end for

### Example

#include <iostream>
using namespace std;
void solve( int s ){
int mat[ s ][ s ] = {0};
int i, j, m, n, p, q, num;
num = 1; // start count from 1
i = 0;
j = 0;
m = 0; // row index lower limit
n = s - 1; // row index upper limit
p = 0; // column index lower limit
q = s - 1; // column index upper limit
while ( num <= s * s ) {

// place numbers horizontally left to right
for ( j = p; j <= q; j++ ) {
mat[ m ][ j ] = num;
num = num + 1;
}
m = m + 1;

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

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

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

// display the mat
for ( i = 0; i < s; i++ ) {
for ( j = 0; j < s; j++ ) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
int main(){
int n = 5;
cout << "Spiral numbers for " << n << " lines." << endl;
solve( n );
}


### Output

Spiral numbers for 5 lines.
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


### Output (With n = 12)

Spiral numbers for 12 lines.
1	2	3	4	5	6	7	8	9	10	11	12
44	45	46	47	48	49	50	51	52	53	54	13
43	80	81	82	83	84	85	86	87	88	55	14
42	79	108	109	110	111	112	113	114	89	56	15
41	78	107	128	129	130	131	132	115	90	57	16
40	77	106	127	140	141	142	133	116	91	58	17
39	76	105	126	139	144	143	134	117	92	59	18
38	75	104	125	138	137	136	135	118	93	60	19
37	74	103	124	123	122	121	120	119	94	61	20
36	73	102	101	100	99	98	97	96	95	62	21
35	72	71	70	69	68	67	66	65	64	63	22
34	33	32	31	30	29	28	27	26	25	24	23


## Conclusion

Displaying number patterns is a fairly common problem while learning programming languages. In this article, we have seen how to display numbers in a square where elements are printed in the spiral form in C++. Starting from the top left corner we move towards right then at the end of n column, we go down, then at the end of n row, go left, and then after reaching the first row go up till 2nd row, then do the same again and again until completing the entire square. Unlike other number pattern problems, it requires a 2D array to solve this problem in an efficient manner.

Updated on: 05-Apr-2023

307 Views 