
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Half Diamond Binary Pattern in Swift
This tutorial will discuss how to write swift program to print half diamond 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 half diamond 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 = 3
Output
The desired output would be ?
0 0 1 0 1 0 0 1 0
METHOD 1- USING NESTED FOR LOOP
We can create a half diamond 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, and nested for loop is used to print binary number pattern.
Example
The following program shows how to print half diamond binary pattern using nested for loop.
import Foundation import Glibc // Size of the pattern let num = 3 // Outer for loop is used to handle the // total number of rows in upper half // diamond binary pattern for m in 1...num { // Printing upper half diamond binary pattern for n in 1...m { if n % 2 == 0 { print(1, terminator : " ") } else { print(0, terminator : " ") } } print("") } // Outer for loop is used to handle the // total number of rows in lower half // diamond binary pattern for p in 1...num-1 { // Printing lower half diamond binary pattern for n in 1...num-p { if n % 2 == 0 { print(1, terminator : " ") } else { print(0, terminator : " ") } } print("") }
Output
0 0 1 0 1 0 0 1 0
Here, in the above code, we use nested for loops to print binary numbers in the half diamond pattern. To print binary numbers, we check remainder. If the 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 half diamond binary pattern using string.init() function.
import Foundation import Glibc // Size of the pattern let num = 6 // Creating upper half diamond binary pattern // Using String.init() function for x in 1...num { print(String.init(repeating: "10", count: x)) } // Creating lower half diamond binary pattern // Using String.init() function for x in 1...num-1 { print( String.init(repeating: "11", count: num-x)) }
Output
10 1010 101010 10101010 1010101010 101010101010 1111111111 11111111 111111 1111 11
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 binary pattern using stride() function.
import Foundation import Glibc // Size of the pattern let num = 8 // Handle the length of the upper half pattern for m in 1...num { // Printing upper half diamond binary 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("") } // Handle the length of the lower half pattern for x in 0...num-1 { // Printing lower half diamond binary pattern // Using stride() function for n in stride(from: num, to: x, 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 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 1 0 1
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.