Swift Program to Print Left Triangle Pattern of Numbers


This tutorial will discuss how to write swift program to print left triangle pattern of numbers.

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 left triangle pattern of numbers, 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 = 10

Output

The desired output would be −

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10

Method 1 - Using Nested For Loop

We can create a left triangle star pattern or any other pattern using nested for loops.

Example

The following program shows how to print a left triangle pattern of numbers using nested for loop.

import Foundation import Glibc // Size of the left triangle pattern of numbers let num = 9 // Handle the length of pattern for x in 1...num{ // Printing left triangle pattern of numbers for y in 1...x{ print(y, terminator : " ") } // New line after each row print(" ") }

Output

1  
1 2  
1 2 3  
1 2 3 4  
1 2 3 4 5  
1 2 3 4 5 6  
1 2 3 4 5 6 7  
1 2 3 4 5 6 7 8  
1 2 3 4 5 6 7 8 9  

Here, in the above code, we uses nested for loops to print left triangle pattern of numbers. The outer most for loop(starts from 1 to 9) is use to handle the total number of rows are going to print and each row is start with new line. Now the nested for loop(starts from 1 to x) is used to print numbers from 1 to 9 in triangle pattern or we can say is used to handle the total number of columns in the pattern.

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 left triangle pattern of numbers using string.init() function.

import Foundation import Glibc // Size of the left triangle pattern of numbers let num = 4 // Handle the length of pattern for i in 1...num{ // Printing left triangle pattern of numbers print(String.init(repeating:"123", count:i)) }

Output

123
123123
123123123
123123123123

Here in the above code, we create a left triangle pattern of numeric string = “123” of height 4 using String.init() function. Here we uses for loop(starting from 1 to num) which is used to print each row. In this loop, we uses String.init() function. This function prints “123” according to the count value(that is i) −

print(String.init(repeating:"123", count:i))

So the working of the above code is −

num = 4

In 1st iteration: i = 1

print(String.init(repeating: "123”, count: 1))

So it print one times “123”

print(String.init(repeating: "123”, count: 2))

So it print two times “123”

So on till 4th iteration and print left triangle pattern of numbers.

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 left triangle pattern of numbers using stride() function.

import Foundation import Glibc // Size of the left triangle pattern of numbers let num = 13 // Handle the length of pattern for i in 1...num{ // Printing left triangle pattern of numbers // Using stride() function for j in stride(from: 1, to: i, by: 1){ print(j, terminator:" ") } print("") }

Output

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 
1 2 3 4 5 6 7 8 9 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 11 
1 2 3 4 5 6 7 8 9 10 11 12   

Here in the above code, we uses nested for loops along with stride() function. The outermost for loop(start from 1 to num) is used to handle the total number of rows are going to print and each row starts with a new line. And the nested for loop is used to print left triangle pattern of numbers using stride() function −

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

Here the iteration starts from 1 to i and each iteration is increased by one and print numbers from 1 to 12 in left triangle pattern.

Updated on: 03-Nov-2022

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements