Swift program to Print Mirror Lower Star Triangle Pattern


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

To create a mirror lower star triangle 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 mirror lower star triangle 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.

For lower 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 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 lower star triangle pattern.

For mirror lower 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 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 mirror lower star triangle pattern.

Example

The following program shows how to print mirror lower star triangle 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 lower star triangle pattern 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 // lower star triangle pattern for _ in 1...num-i{ print("*", terminator: "") } // Add new line print("") } // Outer for loop is used to handle the total // number of rows in mirror lower 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 // mirror lower star triangle pattern 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 lower star triangle pattern using init() function −

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

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

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

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

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

Example

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

import Foundation import Glibc // Length of the triangle pattern var num = 4 var x = num // Creating lower star triangle pattern // Using String.init() function while x >= 1{ print(String.init(repeating: " ", count: num-x) + String.init(repeating: "*", count: x)) x -= 1 } // Creating mirror lower star triangle pattern // Using String.init() function for y in 1...num{ print(String.init(repeating: " ", count: num-y) + String.init(repeating: "*", count: y)) }

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

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

Output

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

Updated on: 03-Nov-2022

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements