How to print Hollow Inverted Pyramid Pattern in Go - Step by Step Examples
How to print Hollow Inverted Pyramid Pattern in Go ?
Answer
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.
✐ Examples
1 Print Hollow Inverted Pyramid Pattern
In this example,
- We define a function named
printHollowInvertedPyramid
that takes an integern
as a parameter representing the height of the pyramid. - We use a loop to iterate through the rows of the pyramid from
0
ton-1
. - Inside the loop, we print the leading spaces for each row to align the pyramid shape using another loop.
- We use another loop to handle the columns, where we conditionally print an asterisk
*
or a space - If it's the first or last column of the row, or if it's the first or last row, we print an asterisk.
- Otherwise, we print a space.
- We print a newline character after each row to move to the next line.
- In the
main
function, we call theprintHollowInvertedPyramid
function with a specific height valuen
.
Go Program
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)
}
Output
********* * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Inverted 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 ?