Swift Program to Print Downward Triangle Binary Pattern


This tutorial will discuss how to write swift program to print downward triangle binary 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 downward triangle binary 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 = 5

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 a downward triangle binary 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, 1st nested for loop is used for white spaces, and 2nd nested for loop is used to print binary pattern.

Example

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

import Foundation 
import Glibc

// Size of the pattern 
let num = 3

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

   // Nested for loop is used to print white 
   // spaces 
   for _ in 0...m { 
      print(" ",terminator: " ") 
   }
   
   // Printing downward triangle binary pattern 
   for n in 0...num-m { 
      if n % 2 == 0 { 
         print(1, terminator : " ") 
      } 
      else { 
         print(0, terminator : " ") 
      } 
   } 
   print("")
}

Output

 1 0 1 0 
    1 0 1 
      1 0 
        1

Here, in the above code, we uses nested for loops to print downward triangle binary pattern. The outer most for loop(starts from 0 to 3) 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 m) is used to print the white spaces and in each iteration the white space is increase by one. And the second nested for loop is used to print downward triangle using binary numbers. Here we print binary number 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 downward triangle binary pattern using string.init() function.

import Foundation
import Glibc

// Size of the pattern 
let num = 6 
var x = num

// Creating downward triangle binary pattern 
// Using String.init() function 
while x >= 1 { 
   print(String.init(repeating: " ", count: num-x) + String.init(repeating: "10", count: x)) 
   x -= 1 
}

Output

101010101010
 1010101010
  10101010
   101010
    1010
     10

Here in the above code, we create a downward triangle binary pattern of height 6 using String.init() function. Here we use while loop(x >= 1) which starts from 6 and in each iteration the value of x is decrease by one. So String.init(repeating: " ", count: num-x) is used to print spaces, here in every iteration the init() function repeats the white spaces according to the count value(that is num-x) and in every iteration the white space is increased by one. And String.init(repeating: "10", count:x) is used to print “10” in downward triangle binary pattern, here in each iteration init() function repeats “10” according to the value of count(that is x).

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 downward triangle binary 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: 0, to: m, by: 1) {
      print(" ", terminator : " ") 
   }
   
   // Printing downward triangle binary pattern 
   // Using stride() function 
   for n in stride(from: m, to: num, by: 1) { 
      if n % 2 == 0 { 
         print(1, terminator : " ") 
      } 
      else { 
         print(0, terminator : " ") 
      } 
   } 
   print("") 
}

Output

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

Here in the above code, we use three nested for loops. The outermost for loop(starts from 0 to 8) is used to handle the total number of rows are going to print and each row starts with a new line. The first nested for-loop is used to print white spaces and in each iteration the white space is increased by one.

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

The second nested for loop is used to print binary number in downward triangle pattern. Here stride() function is used to print binary number. In this function, the iteration started from m to num and in each iteration the value is increase by one.

for n in stride(from: m, to: num, by: 1) { 
   if n % 2 == 0 { 
      print(1, terminator : " ") 
   } 
   else { 
      print(0, terminator : " ") 
   } 
}

Here we find binary number by checking the remainder. If the remainder is equal to 0, then print “1”. Otherwise, print “0”.

Updated on: 30-Nov-2022

112 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements