Swift program to Print Diamond Star Pattern


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

To create a diamond star pattern, we can use any of the following methods −

  • Using nested for loop

  • Using init() Function

  • Using stride Function

Here we divide the diamond in two portions upper half pyramid and lower half pyramid to solve the problem.

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 diamond star pattern or any other pattern using nested for loops. Here each for loop handle different tasks like holding rows, columns, whitespaces, etc.

Example

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

import Foundation import Glibc // Length of the triangle pattern let num = 5 // Upper pyramid // Outer for loop is used to handle the // total number of rows in upper pyramid for i in 1...num{ // Nested for loop is used to print white // spaces for _ in 0..<(num-i){ print(" ", terminator: " ") } // Nested for loop is used to print upper pyramid of "*" for _ in 1...2*i-1{ print("*", terminator: " ") } // Add new line print("") } // lower pyramid // Outer for loop is used to handle the // total number of rows in lower pyramid 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 pyramid of "*" for _ in 1...(2*num - 2*i - 1){ 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.

Example

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

import Foundation import Glibc // Length of the diamond var num = 8 var x = 1 // Handle the rows of the diamond while x <= num{ // For upper half of the diamond // Checking for even length if num%2 == 0{ if x <= num/2{ let mystar = String.init(repeating: "*", count:(x-1)*2+1) let spaceing = String.init(repeating: " ", count:((num/2)-x)) print(spaceing+mystar) } } // Checking for odd length else{ if x < num/2+1{ let mystars = String.init(repeating: "*", count:(x-1)*2+1) let myspace = String.init(repeating: " ", count:((num/2)-x+1)) print(myspace+mystars) } } // For lower half of the diamond if x > num/2{ let star = String.init(repeating: "*", count:(num-x)*2+1) let mspace = String.init(repeating: " ", count:(x-((num/2)+1))) print(mspace+star) } x+=1 }

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

import Foundation import Glibc // Length of the diamond var num = 8 // Print the upper half of the diamond for x in 1...num{ if x % 2 != 0{ // Print white spaces for _ in stride(from: num, to: x, by: -1){ print(terminator : " ") } // Print star pattern for _ in 1...x{ print("*", terminator : " ") } print("") } } // Print the lower half of the diamond for x in stride(from: num, to: 1, by: -1){ if x % 2 != 0{ // Print white spaces for _ in stride(from: num, to: x-2, by: -1){ print(terminator : " ") } // Print star pattern for _ in stride(from: 2, to: x, by: 1){ print("*", terminator : " ") } print("") } }

Output

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

Updated on: 03-Nov-2022

568 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements