Swift program to Print Pyramid Star Pattern


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

  • Using nested for loop

  • Using init() Function

  • Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 5

Output

The desired output would be −

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

Method 1 - Using Nested For Loop

We can create a pyramid of “*” or any other pattern using nested for loops. Here each for loop handle different tasks such as outermost for loop is used for new rows, 1st nested for loop is used for white spaces, and 2nd nested for loop is used for “*”.

Example

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

import Foundation import Glibc // Height of the pyramid let num = 7 // Outer for loop is used to handle the // total number of rows in pyramid for i in 1...num{ // Nested for loop is used to print white // spaces for _ in 0..<(num-i){ print(" ", terminator: "") } // Nested for loop is used to print pyramid of "*" for _ in 1...2*i-1{ print("*", terminator: "") } // Add new line print("") }

Output

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

Here, in the above code, we uses nested for loops to print pyramid of “*”. The outer most for loop(starts from 1 to 7) is use to handle the total number of rows are going to print and each row is start with new line. Now the first nested for loop(starts form 0 to less than num-i) is used to print the white spaces and in each iteration the white space is decreased by one. And the second nested for loop is used to print “*”. As we can see from the output the “*” are the print in odd number, so this loop starts from 1 to 2*i-1 to print the desired result.

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 pyramid star pattern using string.init() function.

import Foundation import Glibc // Height of the pyramid var num = 8 // Creating pyramid star pattern // Using String.init() function for x in 1...num{ print(String.init(repeating: " ", count: num-x) + String.init(repeating: "*", count: (2*x) - 1)) }

Output

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

Here in the above code, we create a pyramid of height 8 using String.init() function. Where String.init(repeating: " ", count: num-x) is used to print spaces, here in every iteration the init() function repeats the space according to the count value(that is num-x) and in every iteration the value of count is decrease by x in downward direction. And String.init(repeating: "*", count: (2*x) - 1) is used to print “*” in pyramid pattern, here in each iteration init() function repeats “*” according to the value of count(that is (2*x) - 1). So the working of the above code is −

num = 8

In 1st iteration x = 1 −

print(String.init(repeating: " ", count: 8-1) + String.init(repeating: "*", count: (2*1) - 1))

So it print 7 spaces and 1 “*”

In 2nd iteration x = 2 −

print(String.init(repeating: " ", count: 8-2) + String.init(repeating: "*", count: (2*2) - 1))

So it print 6 spaces and 3 “*”

In 3rd iteration x = 3 −

print(String.init(repeating: " ", count: 8-3) + String.init(repeating: "*", count: (2*3) - 1))

So it print 5 spaces and 5 “*”

…. so on till 8th iteration and print pyramid.

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

Example

The following program shows how to print pyramid star pattern using stride() function.

import Foundation import Glibc // Height of the pyramid let num = 9 for i in 1...num{ // Printing white spaces for _ in stride(from: num, to: i, by: -1){ print(terminator : " ") } // Printing pyramid of "*" for _ in 1...i{ print("*", terminator : " ") } // New line after each row print(" ") }

Output

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

Here in the above code, We uses three nested for loops. The outer most for loop is used to handle the total number of rows are going to print(so this loop print total 9 rows) and each row starts with a new line. The first nested for-loop is used to print white spaces, here stride() function is used to print white spaces. In this function, the iteration started from num to i and in each iteration the value is decreased by one.

for _ in stride(from: num, to: i, by: -1) {
   print(terminator : " ")
}

The second nested for loop is used to print “*” in pyramid pattern: −

for _ in 1...i {
   print("*", terminator : " ") 
}

Updated on: 03-Nov-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements