# Swift Program to Print Upper Binary Triangle Pattern

This tutorial will discuss how to write swift program to print upper star triangle pattern.

Binary pattern is a sequence of “0” and “1” which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc.

To create a upper binary right triangle 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 = 3


Output

The desired output would be −

1
1 0
1 0 1
1 0 1 0


## METHOD 1- USING NESTED FOR LOOP

We can create an upper binary triangle 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 to pattern.

### Example

The following program shows how to print upper binary triangle pattern using nested for loop.

import Foundation
import Glibc

// Size of the pattern
let num = 4

// Handle the length of pattern
for m in 0...num {

// Nested for loop is used to print white
// spaces
for _ in 0..<(num-m) {
print(" ", terminator: " ")
}

// Printing upper Binary triangle pattern
for n in 0...m {
if n % 2 == 0 {
print(1, terminator : " ")
} else {
print(0, terminator : " ")
}
}
print(" ")
}


### Output

        1
1 0
1 0 1
1 0 1 0
1 0 1 0 1


Here, in the above code, we use nested for loops to print upper binary triangle pattern. The outermost for loop(starts from 0 to 4) is use to handle the total number of rows are going to print and each row is start with new line. Now the first nested for loop(starts form 0 to <(num-m)) is used to print the white spaces and in each iteration the white space is decrease by one. And the second nested for loop(starts from 0 to m) is used to print upper binary triangle pattern using remainder method. If remainder is equal to 0 then print 1, otherwise print 0.

## 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 upper star triangle pattern using string.init() function.

import Foundation
import Glibc

// Size of the pattern
let num = 4

// Handle the length of pattern
for i in 1...num {

// Printing binary right triangle pattern
print(String.init(repeating: " ", count: num-i) + String.init(repeating:"01", count:i))
}


### Output

   01
0101
010101
01010101


Here in the above code, we create an upper binary triangle pattern of height 4 using String.init() function. Here we use for loop which starts from 1 to 4. So String.init(repeating: “ ", count: num-i) is used to print spaces, here in every iteration the init() function repeats the white spaces according to the count value(that is num-i). And String.init(repeating: "01", count: i) is used to print “01” in upper binary triangle pattern, here in each iteration init() function repeats “01” according to the value of count(that is i).

## 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 upper binary triangle pattern using stride() function.

import Foundation
import Glibc

// Size of the pattern
let num = 8

// Handle the length of pattern
for m in 0...num {
// Printing white spaces
for _ in stride(from: num, to: m, by: -1) {
print(" ", terminator : " ")
}
// Printing Binary Right triangle pattern
// Using stride() function
for n in stride(from: 1, to: m, by: 1) {
if n % 2 == 0 {
print(1, terminator : " ")
} else {
print(0, terminator : " ")
}
}
print("")
}


### Output

            0
0 1
0 1 0
0 1 0 1
0 1 0 1 0
0 1 0 1 0 1
0 1 0 1 0 1 0


Here in the above code, we uses three nested for loops. The outer most for loop is used to handle the total number of rows are going to print(so this loop print total 7 rows) and each row starts with a new line. The first nested for-loop is used to print white spaces, here stride() function is used to print white spaces. In this function, the iteration started from num to m and in each iteration the value is decreased by one. The second nested for loop is used to print upper binary triangle pattern using “1” and “0”. Here we find binary value using remainder method. If remainder is equal to 0 then print 1, otherwise print 0.

Updated on: 30-Nov-2022

138 Views