Swift Program to Print Hollow Right Triangle Star Pattern


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

  • Using nested for loop
  • Using stride Function

Below is a demonstration of the same −

Input

Suppose our given input is −

Num = 5

Output

The desired output would be −

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

Algorithm

Following is the algorithm −

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

Step 2 − Run nested for loop to print spaces and “*” in the pattern. Here the outer for-loop handle rows and inner for-loop handle column.

Step 3 − Inside the nested for loop if ((j == 1) || (j == i) || (i == num)) is true, then print “*”. Otherwise print space.

Step 4 − Print the output

Method 1- Using nested for loop

We can create a hollow right 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, nested for loop handle columns, etc.

Example

The following program shows how to print hollow right triangle star pattern using nested for loop.

import Foundation
import Glibc
var num = 5
// Print rows
for i in 1...num{
   // Print columns
   for j in 1...i{
      // Print "*" in hollow right angle triangle
      if ((j == 1) || (j == i) || (i == num)){
         print("*", terminator: "")
      }
      else{
         print("", terminator: " ")
      }
   }
   // Print New line
   print("")
}

Output

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

Here, in the above code, we use nested for loops to print hollow right triangle star pattern. The outermost for loop(starts from 1 to 5) is used to handle the total number of rows that are going to print and each row starts with a new line. The nets for loop handle the number of columns. In this nested loop, we use the following three conditions to print “*” −

  • if(j == 1) is used to print first column
  • if(j == i) is used to print second column
  • if(i == num) is used to print last column

If all the conditions are not satisfied then print white space.

Method 2- Using stride Function

Swift provides an in-built function named stride(). The stride() function is used to move from one value to another with increments or decrements. Or we can say the stride() function returns a sequence from the starting value but does not include the 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 hollow right triangle star pattern using stride() function.

import Foundation
import Glibc
 
var num = 10

// Print rows
for i in stride(from:1, to:num+1, by:1){
   // Print columns
   for j in stride(from:1, to:i+1, by:1){
      // Print "*" in hollow right angle triangle
      if ((j == 1) || (j == i) || (i == num)){
         print("*", terminator: "")
      }
      else{
         print("", terminator: " ")
      }
   }
   // Print New line
   print("")
}

Output

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

Here, in the above code, we uses stride() function in the for loop to print hollow right angled triangle pattern.

Updated on: 13-Dec-2022

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements