Swift Program to Print Solid Square Star Pattern


This tutorial will discuss how to write swift program to print solid square 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 solid square 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 = 4

Output

The desired output would be −

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

Method 1 - Using Nested For Loop

We can create a solid square star 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, and nested for loop is used for “*”.

Example

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

import Foundation import Glibc // Size of the solid square star pattern let num = 6 // Handle the length of pattern for _ in 1...num{ // Printing solid square star pattern for _ in 1...num{ print("*", terminator : " ") } // New line after each row print(" ") }

Output

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

Here, in the above code, we uses nested for loops to print solid square star pattern. The outer most for loop(starts from 1 to 6) is use to handle the total number of rows are going to print and each row is start with new line. Now the nested for loop(starts from 0 to 6) is used to print the “*” in solid square pattern.

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

import Foundation import Glibc // Size of the solid square star pattern let num = 6 // Handle the length of pattern for _ in 1...num{ // Printing solid square star pattern print(String.init(repeating:"*", count:num)) }

Output

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

Here in the above code, we create a solid square star pattern of height 6 using String.init() function. Here we uses for loop(starting from 1 to 6) which is used to print each row. In this loop, we uses String.init() function. This function prints “*” according to the count value(that is 6). So the working of the above code is −

num = 6

In 1st iteration −

print(String.init(repeating: "*”, count: 6))

So it print 6 “*”

In 2nd iteration −

print(String.init(repeating: "*”, count: 6))

So it print 6 “*”

…. so on till 6th iteration and print solid square star pattern.

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 solid square star pattern using stride() function.

import Foundation import Glibc // Size of the solid square star pattern let num = 8 // Handle the length of pattern for _ in 1...num{ // Printing solid square star pattern // Using stride() function for _ in stride(from: 0, to: num, by: 1){ print("*", terminator:" ") } print("") }

Output

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

Here in the above code, we use nested for loops. The outermost for loop (starts from 1 to 8) is used to handle the total number of rows are going to print and each row starts with a new line. The nested for loop is used to print solid square star pattern using stride() function −

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

Here the iteration starts from 0 to num and each iteration increased by one and print “*” in solid square pattern.

Updated on: 03-Nov-2022

355 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements