Swift program to Print Sandglass Star Pattern


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

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

To create a sandglass star pattern, we can use any of the following methods −

  • Using nested for loop

  • Using init() Function

  • Using stride Function

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

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 4

Output

The desired output would be −

* * *
 * *
  *
  *
 * *
* * *

Method 1 - Using Nested For Loop

We can create a sandglass 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 sandglass 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-i. 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 i. This loop is used print lower half sandglass pattern.

Example

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

import Foundation import Glibc // Length of the triangle pattern let num = 8 // 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 sandglass for _ in 1...num-i{ print("*", terminator: " ") } // Add new line print("") } // Outer for loop is used to handle the total // number of rows in lower half portion for i in 1..<num{ // Nested for loop is used to print white // spaces for _ in 1...(num-i){ print(terminator: " ") } // Nested for loop is used to print // lower half sand glass for _ in 1...i{ print("*", terminator: " ") } // Add new line print("") }

Output

 * * * * * * * 
  * * * * * * 
   * * * * * 
    * * * * 
     * * * 
      * * 
       * 
       * 
      * * 
     * * * 
    * * * * 
   * * * * * 
  * * * * * * 
 * * * * * * * 

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.

Algorithm

Following is the algorithm −

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

Step 2 − Run 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: "*", count: 2*x-1))
   x -= 1
}

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

Step 3 − 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: "*", count: 2*y-1))
}

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

Example

The following program shows how to print the sandglass 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: "*", 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: "*", count: 2*y-1)) }

Output

*******
 *****
  ***
   *
   *
  ***
 *****
*******

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

Example

The following program shows how to print sandglass star 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{ // Print spaces for _ in 1...x{ print(terminator: " ") } // Print "*" for _ in stride(from: x, to: num+1, by: 1){ print("*",terminator : " ") } // Add new lines print("") } // Creating lower half portion // Handle length of the lower half portion for x in 1...num{ // Print spaces for _ in stride(from: num+1, to: x, by: -1){ print(terminator : " ") } // Print "*" for _ in 1...x{ print("*",terminator : " ") } // Add new line print(" ") }

Output

 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 
      *  
     * *  
    * * *  
   * * * *  
  * * * * *  
 * * * * * *    

Updated on: 03-Nov-2022

645 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements