Swift Program to Print Reverse Numeric Pyramid Pattern


This tutorial will discuss how to write swift program to print reverse 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 reverse 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 = 5

Output

The desired output would be −

1 2 3 4 
 1 2 3 
  1 2 
   1

Method 1 - Using Nested For Loop

We can create a reverse pyramid of numbers or any other pattern using nested for loops. Here each for loop handle different tasks.

Example

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

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

Output

 1 2 3 4 5 6 
  1 2 3 4 5 
   1 2 3 4 
    1 2 3 
     1 2 
      1 

Here, in the above code, we use nested for loops to print reverse numeric pyramid. The outer most for loop(starts from 1 to 6) 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 i) is used to print the white spaces and in each iteration the white space is increase by one. And the second nested for loop is used to print numbers from 1 to 6.

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

import Foundation import Glibc // Height of the reverse numeric pyramid var num = 4 var x = num // Creating reverse numeric pyramid star pattern // Using String.init() function while x >= 1{ print(String.init(repeating: " ", count: num-x) + String.init(repeating: "123", count: (2*x) - 1)) x -= 1 }

Output

123123123123123123123
 123123123123123
  123123123
   123

Here in the above code, we create a reverse numeric pyramid of height 4 using String.init() function. Here we use while loop(x >= 1) which starts from 4 and in each iteration the value of x is decrease by one. So String.init(repeating: " ", count: num-x) is used to print spaces, here in every iteration the init() function repeats the white spaces according to the count value(that is num-x). And String.init(repeating: "123", count: (2*x) - 1) is used to print “123” in reverse numeric pyramid pattern, here in each iteration init() function repeats numeric string: “123” according to the value of count(that is (2*x) - 1). So the working of the above code is −

num = 4

In 1st iteration x = 4 −

print(String.init(repeating: " ", count: 4-4) + String.init(repeating: "*", count: (2*4) - 1))

So it print 0 spaces and 7 times “123”

In 2nd iteration x = 3 −

print(String.init(repeating: " ", count: 4-3) + String.init(repeating: "*", count: (2*3) - 1))

So it print 1 spaces and 7 times “123”

….so on till 4th iteration and print reverse numeric pyramid.

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

import Foundation import Glibc // Height of the reverse numeric pyramid let num = 9 for i in stride(from: num, to: 0, by: -1){ // Printing white spaces for _ in stride(from: num, to: i, by: -1){ print(terminator : " ") } // Printing reverse numeric pyramid for j in stride(from: 1, to: i+1, by: 1){ print(j, terminator : " ") } // New line after each row print(" ") }

Output

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

Here in the above code, we uses three nested for loops. The outermost for loop(starts from 1 to 9) is used to handle the total number of rows are going to print and each row starts with a new line. The first nested for-loop is used to print white spaces and in each iteration the white space is increased by one using stride() function −

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. Here stride() function is used to print numbers. In this function, the iteration started from 1 to 1+1 and in each iteration the value is increase by one and print numbers −

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

Updated on: 03-Nov-2022

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements