# Swift program to print spiral pattern

This tutorial will discuss how to write swift program to print spiral pattern.

Numeric pattern is a sequence of numbers which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc. These numeric patterns are generally used to understand or practice the program flow controls, also they are good for logical thinking.

To create a spiral pattern of numbers, we can use any of the following methods −

• Using nested for loop
• Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 6


Output

The desired output would be −

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


## Method 1 - Using nested for loop

We can create a left triangle star pattern or any other pattern using nested for loops.

### Example

The following program shows how to print spiral pattern of numbers using nested for loop.

import Swift
import Foundation

var number = 4
var patternSize = 2 * number - 1

// Handle each row
for x in 1...patternSize{

// Printing the number in spiral pattern
for y in 1...patternSize{
print(max(abs(x-number), abs(y-number)) + 1, terminator: " ")
}
print(" ")
}


### 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


Here, in the above code, we uses nested for loops to print spiral pattern of numbers. The outer most for loop(starts from 1 to patternSize) is use to handle the total number of rows are going to print and each row is start with new line. Now the nested for loop(starts from 1 to patternSize) is used to print spiral pattern of numbers.

## Method 2 - Using Stride Function

Swift provide an in-built function named stride(). The stride() function is used to move from one value to another with increment or decrement. Or we can say stride() function return a sequence from the starting value but not include end value and each value in the given sequence is steps by the given amount.

### Syntax

Following is the syntax −

stride(from:startValue, to: endValue, by:count)


Here,

from − Represent the starting value to used for the given sequence.

to − Represent the end value to limit the given sequence

by − Represent the amount to step by with each iteration, here positive value represent upward iteration or increment and negative value represent the downward iteration or decrement.

### Example

The following program shows how to print spiral pattern of numbers using stride() function.

import Swift
import Foundation

var number = 7
var patternSize = 2 * number - 1

// Handle each row
for x in stride(from:1, to:patternSize+1, by: 1){

// Printing the number in spiral pattern
for y in stride(from:1, to:patternSize+1, by: 1){
print(max(abs(x-number), abs(y-number)) + 1, terminator: " ")
}

print(" ")
}


### Output

7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7


Here in the above code, we uses nested for loops along with stride() function. The outermost for loop(start from 1 to patternSize+1) is used to handle the total number of rows are going to print and each row starts with a new line. And the nested for loop is used to print spiral pattern of numbers using stride() function −

for y in stride(from:1, to:patternSize+1, by: 1){
print(max(abs(x-number), abs(y-number)) + 1, terminator: " ")
}


Here the iteration starts from 1 to patternSize and each iteration is increased by one and print numbers from 1 to 7 in spiral pattern.

Updated on: 13-Dec-2022

270 Views