To print a hollow pyramid pattern in Go, 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,
package main
import (
"fmt"
"strings"
)
func printHollowPyramid(rows int) {
for i := 1; i <= rows; i++ {
pattern := strings.Repeat(" ", (rows-i)*2)
for j := 1; j <= 2*i-1; j++ {
if j == 1 || j == 2*i-1 || i == rows {
pattern += "* "
} else {
pattern += " "
}
}
fmt.Println(pattern)
}
}
func main() {
printHollowPyramid(5)
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Go language with well detailed examples.