# Swift program to Print Numeric Hourglass Pattern

This tutorial will discuss how to write swift program to print numeric hourglass 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 numeric hourglass star pattern we can use any of the following methods −

• Using nested for loop

• Using init() Function

• Using stride Function

Here we divide the numeric hourglass pattern in two portions upper half portion and lower half portion to solve the problem.

## Method 1 - Using Nested For Loop

We can create a numeric hourglass pattern or any other pattern using nested for loops. Here each for loop handle different tasks like holding rows, columns, whitespaces, etc.

### Algorithm

Following is the algorithm −

Step 1 − Declare variable to store the length of the triangle pattern.

Step 2 − Run an outer for loop from 1 to num-1. This loop handle the total number of rows are going to print and each row is start with new line.

Step 3 − Run a nested for loop from 1 to i. This loop is used to print white space and in each iteration the white space is increase by one.

Step 4 − Run another nested for loop from 1 to num-i. This loop is used print upper half numeric hourglass pattern.

Step 5 − Again run an outer for loop from 1 to num-1

Step 6 − Run a nested for loop from 1 to num-m. This loop is used to print white space and in each iteration the white space is decrease by one.

Step 7 − Run another nested for loop from 1 to m. This loop is used print lower half numeric hourglass pattern.

### Example

The following program shows how to print numeric hourglass pattern using nested for loop.

import Foundation
import Glibc

// Length of the triangle pattern
let num = 4

// Outer for loop is used to handle the total
// number of rows in upper half portion
for i in 1..<num{

// Nested for loop is used to print white
// spaces
for _ in 1...i{
print(terminator: " ")
}

// Nested for loop is used to print
// upper half hourglass
for j in 1...num-i{
print(j, terminator: " ")
}

print("")
}

// Outer for loop is used to handle the total
// number of rows in lower half portion
for m in 1..<num{

// Nested for loop is used to print white
// spaces
for _ in 1...(num-m){
print(terminator: " ")
}

// Nested for loop is used to print
// lower half hourglass
for n in 1...m{
print(n, terminator: " ")
}

print("")
}


### Output

 1 2 3
1 2
1
1
1 2
1 2 3


## Method 2 - Using init() Function

Swift provide an in-built function named String.init(). Using this function, we can able to create any pattern. String.init() function create a string in which the given character is repeated the specified number of times.

### Syntax

Following is the syntax −

String.init(repeating:Character, count: Int)


Here, repeating represent the character which this method repeats and count represent the total number of time the given character repeat in the resultant string.

### Example

The following program shows how to print the numeric hourglass pattern using string.init() function.

import Foundation
import Glibc

// Length of the triangle pattern
var num = 4
var x = num

// Creating upper half portion
// Using String.init() function
while x >= 1{
print(String.init(repeating: " ", count: num-x) + String.init(repeating: "33", count: 2*x-1))
x -= 1
}

// Creating lower half portion
// Using String.init() function
for y in 1...num{
print(String.init(repeating: " ", count: num-y) + String.init(repeating: "44", count: 2*y-1))
}


### Output

33333333333333
3333333333
333333
33
44
444444
4444444444
44444444444444


Here, in the above code, we use a while loop with condition x>=1. This loop prints the upper half portion of the sandglass pattern using init() function −

while x >= 1 {
print(String.init(repeating: " ", count: num-x) + String.init(repeating: "33", count: 2*x-1))
x -= 1
}


Here, String.init(repeating: " ", count: num-x) print the white spaces whereas String.init(repeating: "33", count: 2*x-1) print the upper half numeric hourglass pattern.

Now run another for loop from 1 to num. This loop prints the lower half portion of the sandglass pattern using init() function −

for y in 1...num {
print(String.init(repeating: " ", count: num-y) + String.init(repeating: "44", count: 2*y-1))
}


Here, String.init(repeating: " ", count: num-y) print white spaces whereas String.init(repeating: "44", count: 2*y-1) print lower half of the numeric hourglass pattern.

## Method 3 - 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 use 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 represents upward iteration or increment and negative value represent the downward iteration or decrement.

### Example

The following program shows how to print numeric hourglass pattern using stride() function.

import Foundation
import Glibc

// Length of the triangle pattern
var num = 6

// Creating upper half portion
// Handle length of the upper half portion
for x in 1...num-1{

// Print spaces
for _ in 1...x{
print(terminator: " ")
}
// Print numbers from 1 to 5
for y in stride(from: x, to: num, by: 1){
print(y, terminator : " ")
}

print(" ")
}

// Creating lower half portion
// Handle length of the lower half portion
for m in stride(from:num, to: 1, by: -1){

// Print spaces
for _ in 1..<m{
print(terminator : " ")
}

// Print numbers from 5 to 1
for n in stride(from: m-1, to: num, by:1){
print(n, terminator : " ")
}

print(" ")
}


### Output

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


Updated on: 03-Nov-2022

264 Views