How to print Rhombus Pattern in Swift - Step by Step Examples



How to print Rhombus Pattern in Swift ?

Answer

To print a rhombus pattern in Swift, you can use nested loops to control the number of spaces and asterisks printed on each line. The pattern is symmetrical and consists of an upper and lower part.



✐ Examples

1 Rhombus pattern of height 5

In this example,

  1. We start by defining a constant n with a value of 5, which represents the height of the rhombus.
  2. Next, we use a for loop to iterate from 1 to n. This loop controls the number of rows in the upper part of the rhombus.
  3. Within the outer loop, we initialize an empty string row to build the content of the current row.
  4. We then use another for loop to add spaces to the row string. The number of spaces is equal to n - i, where i is the current row number. This ensures that the rhombus is centered.
  5. After adding the spaces, we use another for loop to add asterisks to the row string. The number of asterisks is equal to n, which creates the width of the rhombus.
  6. We then print the row string, which represents the current row of the upper part of the rhombus.
  7. To create the lower part of the rhombus, we use another for loop to iterate from 1 to n-1.
  8. Within this outer loop, we initialize an empty string row to build the content of the current row.
  9. We then use another for loop to add spaces to the row string. The number of spaces is equal to i, where i is the current row number. This ensures that the rhombus is centered.
  10. After adding the spaces, we use another for loop to add asterisks to the row string. The number of asterisks is equal to n, which creates the width of the rhombus.
  11. We then print the row string, which represents the current row of the lower part of the rhombus.

Swift Program

let n = 5
for i in 1...n {
    var row = ""
    for _ in 0..<(n - i) {
        row += " "
    }
    for _ in 0..<n {
        row += "*"
    }
    print(row)
}
for i in 1..<n {
    var row = ""
    for _ in 0..<i {
        row += " "
    }
    for _ in 0..<n {
        row += "*"
    }
    print(row)
}

Output

    *****
   *****
  *****
 *****
*****
 *****
  *****
   *****
    *****

2 Rhombus pattern of height 3

In this example,

  1. We start by defining a constant n with a value of 3, which represents the height of the rhombus.
  2. Next, we use a for loop to iterate from 1 to n. This loop controls the number of rows in the upper part of the rhombus.
  3. Within the outer loop, we initialize an empty string row to build the content of the current row.
  4. We then use another for loop to add spaces to the row string. The number of spaces is equal to n - i, where i is the current row number. This ensures that the rhombus is centered.
  5. After adding the spaces, we use another for loop to add asterisks to the row string. The number of asterisks is equal to n, which creates the width of the rhombus.
  6. We then print the row string, which represents the current row of the upper part of the rhombus.
  7. To create the lower part of the rhombus, we use another for loop to iterate from 1 to n-1.
  8. Within this outer loop, we initialize an empty string row to build the content of the current row.
  9. We then use another for loop to add spaces to the row string. The number of spaces is equal to i, where i is the current row number. This ensures that the rhombus is centered.
  10. After adding the spaces, we use another for loop to add asterisks to the row string. The number of asterisks is equal to n, which creates the width of the rhombus.
  11. We then print the row string, which represents the current row of the lower part of the rhombus.

Swift Program

let n = 3
for i in 1...n {
    var row = ""
    for _ in 0..<(n - i) {
        row += " "
    }
    for _ in 0..<n {
        row += "*"
    }
    print(row)
}
for i in 1..<n {
    var row = ""
    for _ in 0..<i {
        row += " "
    }
    for _ in 0..<n {
        row += "*"
    }
    print(row)
}

Output

  ***
 ***
***
 ***
  ***

Summary

In this tutorial, we learned How to print Rhombus Pattern in Swift language with well detailed examples.




More Swift Pattern Printing Tutorials

  1. How to print Left Half Pyramid Pattern in Swift ?
  2. How to print Right Half Pyramid Pattern in Swift ?
  3. How to print Pyramid Pattern in Swift ?
  4. How to print Rhombus Pattern in Swift ?
  5. How to print Diamond Pattern in Swift ?
  6. How to print Hour Glass Pattern in Swift ?
  7. How to print Hollow Square Pattern in Swift ?
  8. How to print Hollow Pyramid Pattern in Swift ?
  9. How to print Hollow Inverted Pyramid Pattern in Swift ?
  10. How to print Hollow Diamond Pattern in Swift ?
  11. How to print Floyd's Trianlge Pattern in Swift ?
  12. How to print Pascal's Triangle Pattern in Swift ?