Swift Program to Print Alphabetic Right Triangle Pattern


This tutorial will discuss how to write swift program to print alphabetic right triangle pattern.

Alphabetic pattern is a sequence of alphabets starting from A to Z which is used to develop different patterns or shapes like pyramid, rectangle, cross, etc of alphabets.

To create a alphabetic 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 −

Length = A to D

Output

The desired output would be −

A 
A B 
A B C 
A B C D

METHOD 1- USING NESTED FOR LOOP

We can create an alphabetic right 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 print alphabets starting from A to Z in columns.

Example

The following program shows how to print alphabetic right triangle pattern using nested for loop.

import Foundation 
import Glibc

// Finding the ASCII value of the alphabets 
var N1 = Unicode.Scalar("A").value 
var N2 = Unicode.Scalar("M").value

// Run nested for loop to print 
// alphabetic right triangle pattern 
for i in N1...N2 { 
   for j in N1...i { 
      print(UnicodeScalar(j)!, terminator: "") 
   } 
   print("") 
}

Output

A
AB
ABC
ABCD
ABCDE
ABCDEF
ABCDEFG
ABCDEFGH
ABCDEFGHI
ABCDEFGHIJ
ABCDEFGHIJK
ABCDEFGHIJKL
ABCDEFGHIJKLM

Here, in the above code, we print an alphabetic right triangle pattern starting from A to M. So to print the pattern first we find the ASCII value of lower and upper limit using the following code −

var N1 = Unicode.Scalar(“A").value // A = 65 
var N2 = Unicode.Scalar(“M").value // M = 77

Now we use nested for loops. The outer most for loop(starts from A to M or 65 to 77) 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 A to i) is used to handle the total number of columns are going to print.

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 alphabetic right triangle pattern using string.init() function.

import Foundation 
import Glibc

// Size of the alphabetic right triangle pattern 
let num = 4

// Handle the length of the pattern 
for i in 1...num {
   // Printing alphabetic right triangle pattern
   print(String.init(repeating:"ABC", count:i)) 
}

Output

ABC
ABCABC
ABCABCABC
ABCABCABCABC

Here in the above code, we create an alphabetic right triangle pattern of length 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 use String.init() function. This function prints “ABC” according to the count value(that is i)

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

So the working of the above code is −

num = 4 In 1st iteration: i = 1 print(String.init(repeating: "ABC”, count: 1))
 So it print 1 “ABC” 
In 2nd iteration: i = 2 
print(String.init(repeating: "*”, count: 2)) 
So it print 2 “ABCABC”
... so on till 4th iteration and print alphabetic right triangle pattern.

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 represents upward iteration or increment and negative value represent the downward iteration or decrement.

Example

The following program shows how to print alphabetic right triangle pattern using stride() function.

import Foundation 
import Glibc

// Finding the ASCII value of the alphabets 
var N1 = Unicode.Scalar("A").value 
var N2 = Unicode.Scalar("G").value

// Handle the length of pattern 
for i in N1...N2 { 
   // Printing alphabetic right triangle pattern 
   // Using stride() function
   for j in stride(from: N1, to: i+1, by: 1) {  
      print(UnicodeScalar(j)!, terminator:" ") 
   } 
   print("") 
}

Output

A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 
A B C D E F G 

Here, in the above code, we print an alphabetic right triangle pattern starting from A to G. So to print the pattern first we find the ASCII value of lower and upper limit using the following code −

var N1 = Unicode.Scalar(“A").value // A = 65 
var N2 = Unicode.Scalar(“G").value // G = 71

Now we uses nested for loops along with stride() function. The outermost for loop(start from A to G) 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 alphabetic right triangle pattern using stride() function −

for j in stride(from: N1, to: i+1, by: 1) {
   print(UnicodeScalar(j)!, terminator:" ") 
}

Here the iteration starts from N1 to i+1 and each iteration is increased by one and print right triangle from A to G pattern.

Updated on: 30-Nov-2022

395 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements