Swift program to print 8 star pattern


This tutorial will discuss how to write swift program to 8 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 8 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 named rows to store the number of columns.

Step 2 − Declare another variable to store rows −

var val = row * 2 - 1

Step 3 − Run outer for loop starting from 0 to val. This loop handle the rows.

Step 4 − Check if (x==1 || x==row || x==val) is true or not.

If the condition is true, then run 1st nested for loop(starts from 1 to row) to iterate through the column. In the nested for loop, if(y==1 || y==row) is true, then print space, otherwise print “*”.

If the condition is false, then run 2nd nested for loop(starts from 1 to row) to iterate through the column. In the nested for loop, if(y==1 || y==row) is true, then print “*”, otherwise print space.

Step 5 − Print the output.

Method 1 - Using Nested for Loop

We can create a 8 pattern 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, etc.

Example

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

var row = 10
var val = row * 2 - 1
for x in 1...val{
   if(x==1 || x==row || x==val){
      for y in 1...row{
         if(y==1 || y==row){
            print(" ", terminator:"")
         }
         else{
            print("*", terminator:"")
         }
      } 
   }
   else{
      for y in 1...row{
         if(y==1 || y==row){
            print("*", terminator:"")
         }
         else{
            print(" ", terminator: "")
         }
      }
   }
   print(" ")
}

Output

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

Method 2 - 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 8 star pattern using stride() function.

import Swift
import Foundation
var row = 9
var k = row * 2 - 1
for i in stride(from:1, to:k+1, by:1){
   if (i==1 || i==row || i==k){
      for j in stride(from:1, to:row+1, by:1){
         if (j==1 || j == row){
            print(" ", terminator:"")
         }
         else{
            print("*", terminator:"")
         }
      } 
   }
   else{
      for j in stride(from:1, to:row+1, by:1){
         if (j==1||j==row){
            print("*", terminator:"")
         }
         else{
            print(" ", terminator: "")
         }
      }
   }
   print(" ")
}

Output

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

Updated on: 13-Dec-2022

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements