How to print Hollow Pyramid Pattern in Swift - Step by Step Examples
How to print Hollow Pyramid Pattern in Swift ?
Answer
To print a hollow pyramid pattern in Swift, you can use nested loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
✐ Examples
1 Hollow Pyramid Pattern (Height: 5) in Swift
In this example,
- Set the number of rows for the pyramid.
- Use nested loops: one for rows, one for spaces, and one for stars.
- In the inner loops, print spaces for the spaces between stars and print stars for the pyramid edges.
- Adjust conditions to print stars only at the pyramid edges and spaces elsewhere to create the hollow effect.
- Print each row to create the hollow pyramid pattern.
Swift Program
func printHollowPyramid(rows: Int) {
for i in 1...rows {
var pattern = ""
for _ in stride(from: 0, to: rows - i, by: 1) {
pattern += " "
}
for _ in stride(from: 0, to: (2 * i) - 1, by: 1) {
if i == rows || _ == 0 || _ == ((2 * i) - 2) {
pattern += "* "
} else {
pattern += " "
}
}
print(pattern)
}
}
printHollowPyramid(rows: 5)
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in Swift language with well detailed examples.
More Swift Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in Swift ?
- How to print Right Half Pyramid Pattern in Swift ?
- How to print Pyramid Pattern in Swift ?
- How to print Rhombus Pattern in Swift ?
- How to print Diamond Pattern in Swift ?
- How to print Hour Glass Pattern in Swift ?
- How to print Hollow Square Pattern in Swift ?
- How to print Hollow Pyramid Pattern in Swift ?
- How to print Hollow Inverted Pyramid Pattern in Swift ?
- How to print Hollow Diamond Pattern in Swift ?
- How to print Floyd's Trianlge Pattern in Swift ?
- How to print Pascal's Triangle Pattern in Swift ?