Swift program to Print Half Diamond Numeric Pattern


This tutorial will discuss how to write swift program to print half diamond numeric pattern.

Numeric pattern is a sequence of numbers which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc. These numeric patterns are generally used to understand or practice the program flow controls, also they are good for logical thinking.

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

  • Using nested for loop

  • Using init() Function

  • Using stride Function

Method 1 - Using Nested For Loop

We can create a half diamond numeric pattern or any other pattern using nested for loops.

Algorithm

Following is the algorithm −

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

For upper half 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 x. This loop is used print upper half diamond numeric 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-q. This loop is used print lower half diamond numeric pattern.

Example

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

import Foundation import Glibc // Height of the half diamond numeric pattern let num = 9 // Outer for loop is used to handle the // total number of rows in upper half // diamond numeric pattern for x in 1...num{ // Nested for loop is used to print // upper half diamond numeric pattern for y in 1...x{ print(y, terminator: "") } // Add new line print("") } // Outer for loop is used to handle the // total number of rows in lower half // diamond numeric pattern for p in 1...num-1{ // Nested for loop is used to print // lower half diamond numeric pattern for q in 1...num-p{ print(q, terminator: "") } // Add new line print("") }

Output

1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678
1234567
123456
12345
1234
123
12
1

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 numeric pattern using init() function:

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

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

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

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

Example

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

import Foundation import Glibc // Height of the half diamond numeric pattern let num = 4 // Creating upper half diamond numeric pattern // Using String.init() function for x in 1...num{ print(String.init(repeating: "22", count: x)) } // Creating lower half diamond numeric pattern // Using String.init() function for y in 1...num-1{ print(String.init(repeating: "33", count: num-y)) }

Output

22
2222
222222
22222222
333333
3333
33

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

import Foundation import Glibc // Height of the half diamond numeric pattern let num = 3 // For upper half diamond numeric pattern for x in 1...num{ // Printing upper half diamond numeric pattern for y in 1...x{ print(y, terminator : "") } // Adding new line print(" ") } // For lower half diamond numeric pattern for m in 1...num-1{ // Printing lower half diamond numeric pattern for n in stride(from: num, to: m, by: -1){ print(n, terminator : "") } // Adding new line print(" ") }

Output

1 
12 
123 
32 
3  

Updated on: 03-Nov-2022

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements