Swift Program to Print Numeric Pyramid Pattern


This tutorial will discuss how to write swift program to print numeric pyramid pattern.

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 numeric pyramid 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 = 7

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

Method 1 - Using Nested For Loop

We can create a numeric pyramid or any other pattern using nested for loops.

Example

The following program shows how to print numeric pyramid pattern using nested for loop.

import Foundation import Glibc // Height of the numeric pyramid let num = 4 // Outer for loop is used to handle the // total number of rows in numeric pyramid for i in 1...num{ // Nested for loop is used to print white // spaces for _ in 0..<(num-i){ print(" ", terminator: "") } // Nested for loop is used to print numeric pyramid for x in 1...i{ print(x, terminator: " ") } // Add new line print("") }

Output

   1 
  1 2 
 1 2 3 
1 2 3 4

Here, in the above code, we uses nested for loops to print pyramid of numbers starting from 1 to 4. The outer most for loop(starts from 1 to 4) 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 less than num-i) is used to print the white spaces and in each iteration the white space is decreased by one. And the second nested for loop is used to print numbers.

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 numeric pyramid pattern using string.init() function.

import Foundation import Glibc // Height of the numeric pyramid var num = 6 // Creating numeric pyramid pattern // Using String.init() function for x in 1...num{ print(String.init(repeating: " ", count: num-x) + String.init(repeating: "121", count: x)) }

Output

     121
    121121
   121121121
  121121121121
 121121121121121
121121121121121121

Here in the above code, we create a pyramid of height 6 using String.init() function. Where String.init(repeating: " ", count: num-x) is used to print spaces, here in every iteration the init() function repeats the space according to the count value(that is num-x) and in every iteration the value of count is decrease by x in downward direction. And String.init(repeating: "121", count: x)) is used to print numeric string: “121” in pyramid pattern, here in each iteration init() function repeats “123” 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 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 numeric pyramid pattern using stride() function.

import Foundation import Glibc // Height of the numeric pyramid let num = 9 for i in 1...num{ // Printing white spaces for _ in stride(from: num, to: i, by: -1){ print(terminator : " ") } // Printing numeric pyramid for x in 1...i{ print(x, 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 three nested for loops. The outer most for loop is used to handle the total number of rows are going to print(so this loop print total 9 rows) and each row starts with a new line. The first nested for-loop is used to print white spaces, here stride() function is used to print white spaces. In this function, the iteration started from num to i and in each iteration the value is decreased by one.

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

The second nested for loop is used to print numbers starting from 1 to 9 in pyramid pattern −

for x in 1...i { 
   print(x, terminator : " ") 
}

Updated on: 03-Nov-2022

725 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements