Swift Program to Print Floyd’s Triangle


Floyd triangle is right angle triangled which is named after Rober Floyd. It is created with the help of natural numbers starting from 1 in the top left corner and then the consecutive numbers are filled in the specified rows.

Example Demonstration

Input

4

Output

1
2 3
4 5 6
7 8 9 10

Here, the size of Floyd’s triangle is 4. So it will print Floyd’s triangle until 4 rows.

In Swift, we can print Floyd’s triangle using the following methods:

  • Using for−in loop

  • Using while loop

  • Using recursive function

Algorithm

Step 1 − Create a user−defined function which takes the total number of rows as a parameter.

Step 2 − This function contains a ”myNum” variable which contains the starting number of the triangle.

Step 3 − Then it runs a nested loop to display all the rows and columns of Floyd’s Triangle.

Step 4 − Define a variable to store the size of Floyd’s triangle.

Step 5 − Call the function and pass the size as a parameter.

Step 6 − Display the output.

Method 1: Floyd’s Triangle Using for−in Loop

In this method, we will display Floyd’s triangle with the help of a for−in loop. Here we run two for−in loops to display rows and columns of Floyd’s triangle.

Example

In the following Swift program, we will print Floyd’s triangle. So for that, we will create a function which takes only one parameter which is the size of the triangle. This function has the myNum variable which keeps track of the number to be printed in the triangle and then runs nested for−in loops to display rows of Floyd’s triangle.

import Foundation
import Glibc

// Function to print Floyd's Triangle
func displayFloydsTriangle(totalRows: Int) {

    var myNum = 1
    for x in 1...totalRows {
        for _ in 1...x {
            print(myNum, terminator: " ")
            myNum += 1
        }
        
        // Move to the next line
        print() 
    }
}

let sizeOfRows = 6

// Calling function
displayFloydsTriangle(totalRows: sizeOfRows)

Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 

Method 2: Floyd’s Triangle Using while Loop

In this method, we will display Floyd’s triangle with the help of a while loop. Here we run two while loops to display rows and columns of Floyd’s triangle.

Example

In the following Swift program, we will print Floyd’s triangle. So for that, we will create a function which takes only one parameter which is the size of the triangle. This function has the myNum variable which keeps track of the number to be printed in the triangle and a cRow variable to keep track of the current row. Then it uses a nested while loop to display numbers in rows and columns forming a Floyd’s triangle.

import Foundation
import Glibc

// Function to print Floyd's Triangle
func displayFloydsTriangle(totalRows: Int) {

    var myNum = 1
    var cRow = 1
    
    while cRow <= totalRows {
        var col = 1
        
        while col <= cRow {
            print(myNum, terminator: " ")
            myNum += 1
            col += 1
        }
        
        print() 
        cRow += 1
    }
}

let sizeOfRows = 7

// Calling function
displayFloydsTriangle(totalRows: sizeOfRows)

Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28

Method 3: Floyd’s Triangle Using Recursion

In this method, we will display Floyd’s triangle with the help of recursion. Where recursion is a process where a function calls itself to complete the task. Here we will call the function recursively to display Flyod’s triangle.

Example

In the following Swift program, we will print Floyd’s triangle using recursion. For that, we will create a recursive function named displayFloydsTriangle(). It takes three parameters: the total number of rows, starting number and the current row. It prints each row with the help of a for−in loop and then it calls itself with the updated parameters for the next row. Finally, we reached the fTriangle() function with the specified number of rows to start printing Floyd’s triangle.

import Foundation
import Glibc

// Function to print Floyds triangle
func displayFloydsTriangle(totalrows: Int, num: Int, cRow: Int) {

    var x = num
    if cRow > totalrows {
        return
    }
    
    for _ in 1...cRow {
        print(x, terminator: " ")
        x += 1
    }
    print()
    
    // Calling function recursively
    displayFloydsTriangle(totalrows: totalrows, num: x, cRow: cRow + 1)
}

func fTriangle(rows: Int) {
    displayFloydsTriangle(totalrows:rows, num: 1, cRow: 1)
}

let triangleSize = 7
fTriangle(rows: triangleSize)

Output

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 

Real−life Usage

The real−life usage of Floyd’s Triangle is:

  • It is used for educational purposes to teach nested loops, pattern printing, recursions, etc.

  • It is also used to analyze patterns in mathematics and computer science.

  • It is also used in mathematics and puzzles.

  • It is also used in Art and design.

Conclusion

So this is how we can print Floyd’s Triangle. It is a great way to understand the working of nested loops such as for−in loops, while loops as well as recursion. We can also create a reverse Floyd’s triangle using the loops and recursions. Also using the above method we can also create any type of Flyod’s triangle like a star triangle, etc., with some minor changes in the code.

Updated on: 08-Sep-2023

107 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements