Swift Program to Print Half Diamond Star Pattern


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

Create a half diamond 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 = 7

Output

The desired output would be −

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

Method 1 - Using Nested For Loop

We can create a half diamond 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 “*”.

Algorithm

Following is the algorithm −

Step 1 − Declare variable to store the height of the half diamond star pattern

For upper half diamond star pattern −

Step 2 − Run an outer for loop from 1 to num. This loop handle the total number of rows are going to print and each row is start with new line.

Step 3 − Run nested for loop from 1 to i. This loop is used print upper half diamond star pattern.

For lower half diamond star pattern −

Step 4 − Again 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 5 − Run another nested for loop from 1 to num-j. This loop is used print lower half diamond star pattern.

Example

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

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

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

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

Here, in each iteration init() function print “*” according to the value of x. For example if x = 2, then init() prints “**”.

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

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

Example

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

import Foundation import Glibc // Height of the half diamond star pattern let num = 9 // Creating upper half diamond star pattern // Using String.init() function for x in 1...num{ print(String.init(repeating: "*", count: x)) } // Creating lower half diamond star pattern // Using String.init() function for x in 1...num-1{ print( 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 represent upward iteration or increment and negative value represent the downward iteration or decrement.

Example

The following program shows how to print half diamond star pattern using stride() function.

import Foundation import Glibc // Height of the half diamond star pattern let num = 9 // For upper half diamond star pattern for i in 1...num{ // Printing upper half diamond star pattern for _ in 1...i{ print("*", terminator : "") } // Adding new line print(" ") } // For lower half diamond star pattern for j in 1...num-1{ // Printing lower half diamond star pattern for _ in stride(from: num, to: j, by: -1){ print("*", terminator : "") } // Adding new line print(" ") }

Output

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

Updated on: 03-Nov-2022

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements