To print a hollow inverted pyramid pattern in Go, you can use nested loops to handle the rows and columns, and conditionally print spaces or asterisks to create the hollow effect.
In this example,
printHollowInvertedPyramid
that takes an integer n
as a parameter representing the height of the pyramid.0
to n-1
.*
or a space
to create the hollow effect:main
function, we call the printHollowInvertedPyramid
function with a specific height value n
.package main
import "fmt"
func printHollowInvertedPyramid(n int) {
for i := 0; i < n; i++ {
for j := 0; j < i; j++ {
fmt.Print(" ")
}
for j := 0; j < 2*(n-i)-1; j++ {
if i == 0 || j == 0 || j == 2*(n-i)-2 {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
func main() {
n := 5
printHollowInvertedPyramid(n)
}
********* * * * * * * *
In this tutorial, we learned How to print Hollow Inverted Pyramid Pattern in Go language with well detailed examples.