How to print Hollow Pyramid Pattern in Go - Step by Step Examples
How to print Hollow Pyramid Pattern in Go ?
Answer
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.
✐ Examples
1 Hollow Pyramid Pattern (Height: 5) in Go
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.
Go Program
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)
}
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in Go language with well detailed examples.
More Go Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in Go ?
- How to print Right Half Pyramid Pattern in Go ?
- How to print Pyramid Pattern in Go ?
- How to print Rhombus Pattern in Go ?
- How to print Diamond Pattern in Go ?
- How to print Hour Glass Pattern in Go ?
- How to print Hollow Square Pattern in Go ?
- How to print Hollow Pyramid Pattern in Go ?
- How to print Hollow Inverted Pyramid Pattern in Go ?
- How to print Hollow Diamond Pattern in Go ?
- How to print Floyd's Trianlge Pattern in Go ?
- How to print Pascal's Triangle Pattern in Go ?