To print a hollow inverted pyramid pattern in Swift, we use nested loops to manage spaces and stars.
In this example,
func printHollowInvertedPyramid(numRows: Int) {
for row in stride(from: numRows, through: 1, by: -1) {
var pattern = ""
for space in 0..<(numRows - row) {
pattern += " "
}
if row == numRows || row == 1 {
pattern += String(repeating: "*", count: 2 * row - 1)
} else {
pattern += "*"
pattern += String(repeating: " ", count: 2 * row - 3)
pattern += "*"
}
print(pattern)
}
}
printHollowInvertedPyramid(numRows: 5)
********* * * * * * * *
In this tutorial, we learned How to print Hollow Inverted Pyramid Pattern in Swift language with well detailed examples.