Triangle Pattern Program Using Number in Swift

Triangle pattern program using number in swift programming language from the basics to understand the pattern program using the numbers to print the pattern programs

String Count in Swift

A collection of characters, such as “Swift,” is referred to as a string. Swift strings are Unicode correct and locale agnostic, as well as being meant to be efficient. String literals and string interpolations can be used to construct new strings. A string literal is a collection of characters encased in quotation marks. Swift strings can be viewed as a collection of single characters. So, you can use yourString.count to count the number of entries in the characters array to get the length of a string.

Count Function Program in Swift

In Swift, use the count function on the array to get its size. Here’s an example of how to count the number of entries in an array quickly. The array variable’s name is array name. The size of this array is returned as an integer by count. Using this program easily can execute triangle pattern program using number in swift

Pattern Program in Swift

Pattern programs are simply patterns made up of integers, alphabets, or symbols in a specific order. The for loop condition can be used to solve these types of pattern programs quickly. There are two or more loops in each pattern program. The number of loops is determined by the pattern or logic’s complexity. The first for loop affects the row, whereas the second affects the column.

func pattern(_ int : Int)
{
for i in 1...int
{
print(String.init(repeating:" ",count:int-i)+
String.init(repeating:"1",count: 2*i - 1))
}
}
pattern(8)

Output:

1

11

111

1111

111111