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.
In this example,
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)
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Swift language with well detailed examples.