Swift program to Print Upper Numeric Triangle Pattern

This tutorial will discuss how to write swift program to print upper numeric triangle 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 upper numeric triangle pattern we can use any of the following methods ?

  • Using nested for loop

  • Using stride Function

Below is a demonstration of the same ?

Input

Suppose our given input is ?

Num = 5

Output

The desired output would be ?

     0
    01
   012
  0123
 01234
012345

Method 1 - Using Nested For Loop

We can create an upper numeric triangle pattern or any other pattern using nested for loops.

Example

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

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Height of the upper numeric triangle pattern</span>
<span class="token keyword">let</span> num <span class="token operator">=</span> <span class="token number">4</span>

<span class="token comment">// Outer for loop is used to handle the</span>
<span class="token comment">// total number of rows in upper numeric</span>
<span class="token comment">// triangle pattern</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>num<span class="token punctuation">{</span>

   <span class="token comment">// Nested for loop is used to print white</span>
   <span class="token comment">// spaces</span>
   <span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token operator"><</span><span class="token punctuation">(</span>num<span class="token operator">-</span>i<span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">,</span> terminator<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   
   <span class="token comment">// Nested for loop is used to print</span>
   <span class="token comment">// upper numeric triangle pattern</span>
   <span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>i<span class="token punctuation">{</span>
      <span class="token function">print</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> terminator<span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   
   <span class="token comment">// Add new line</span>
   <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">""</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

    0
   01
  012
 0123
01234

Here, in the above code, we uses nested for loops to print upper numeric triangle pattern. The outermost for loop(starts from 0 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 <(num-i)) is used to print the white spaces and in each iteration the white space is decrease by one. And the second nested for loop(starts from 0 to i) is used to print upper numeric triangle pattern starts from number 0 to 4.

Method 2 - 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 use 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 upper numeric triangle pattern using stride() function.

<div class="execute"></div><div class="code-mirror  language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc

<span class="token comment">// Height of the upper star triangle pattern</span>
<span class="token keyword">let</span> num <span class="token operator">=</span> <span class="token number">9</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>num<span class="token punctuation">{</span>

   <span class="token comment">// Printing white spaces</span>
   <span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token function">stride</span><span class="token punctuation">(</span><span class="token parameter">from<span class="token operator">:</span> num<span class="token punctuation">,</span> to<span class="token operator">:</span> i<span class="token punctuation">,</span> by<span class="token operator">:</span> <span class="token operator">-</span><span class="token number">1</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
      <span class="token function">print</span><span class="token punctuation">(</span>terminator <span class="token operator">:</span> <span class="token string">" "</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   
   <span class="token comment">// Printing upper star triangle pattern</span>
   <span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token number">1.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>i<span class="token punctuation">{</span>
      <span class="token function">print</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> terminator <span class="token operator">:</span> <span class="token string">""</span><span class="token punctuation">)</span>
   <span class="token punctuation">}</span>
   
   <span class="token comment">// New line after each row</span>
   <span class="token function">print</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

        1 
       12 
      123 
     1234 
    12345 
   123456 
  1234567 
 12345678 
123456789 

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 upper numeric triangle pattern using numbers starting from 1 to 9 ?

for x in 1...i { 
   print(x, terminator : "") 
}
Updated on: 2022-11-03T12:23:37+05:30

548 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements