Swift Program to Print Mirror Upper Star Triangle Pattern


This tutorial will discuss how to write swift program to print mirror upper star triangle 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.

  • 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 mirror upper star triangle pattern 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 “*”.

Algorithm

Following is the algorithm −

Step 1 − Declare variable to store the height of the triangle.

For upper star 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 num-i. This loop is used to print white space and in each iteration the white space is decrease by one.

Step 4 − Run another nested for loop from 1 to i. This loop is used print upper star triangle pattern.

For mirror upper star triangle pattern −

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

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

Step 7 − Run another nested for loop from 1 to num-j. This loop is used print mirror upper star triangle pattern.

Example

The following program shows how to print mirror upper star triangle pattern using nested for loop.

import Foundation import Glibc // Height of the mirror upper star triangle pattern let num = 5 // Outer for loop is used to handle the // total number of rows in upper star // triangle pattern 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 // upper star triangle pattern for _ in 1...i{ print("*", terminator: "") } // Add new line print("") } // Outer for loop is used to handle the // total number of rows in mirror upper star // triangle pattern for j in 1..<num{ // Nested for loop is used to print white // spaces for _ in 1...j{ print(terminator: " ") } // Nested for loop is used to print // mirror upper star triangle pattern for _ in 1...num-j{ 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 height of the triangle.

Step 2 − Run a for loop from 1 to num. This loop prints the upper star triangle pattern using init() function −

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

Here, String.init(repeating: " ", count: num-x) print the white spaces whereas String.init(repeating: "*", count: x) print the upper star triangle pattern.

Step 3 − Run another for loop from 0 to num-1. This loop prints the mirror upper star triangle pattern using init() function:

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

Here, String.init(repeating: " ", count: x) print white spaces whereas String.init(repeating: "*", count: num-x) print mirror upper star triangle pattern

Example

The following program shows how to print upper star triangle pattern using string.init() function.

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

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 mirror upper star triangle pattern using stride() function.

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

Output

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

Updated on: 03-Nov-2022

250 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements