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,

  1. We define a function named printHollowInvertedPyramid that takes an integer n as a parameter representing the height of the pyramid.
  2. We use a loop to iterate through the rows of the pyramid from 0 to n-1.
  3. Inside the loop, we print the leading spaces for each row to align the pyramid shape using another loop.
  4. We use another loop to handle the columns, where we conditionally print an asterisk * or a space to create the hollow effect:
  5. If it's the first or last column of the row, or if it's the first or last row, we print an asterisk.
  6. Otherwise, we print a space.
  7. We print a newline character after each row to move to the next line.
  8. In the main function, we call the printHollowInvertedPyramid function with a specific height value n.

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

  1. How to print Left Half Pyramid Pattern in Go ?
  2. How to print Right Half Pyramid Pattern in Go ?
  3. How to print Pyramid Pattern in Go ?
  4. How to print Rhombus Pattern in Go ?
  5. How to print Diamond Pattern in Go ?
  6. How to print Hour Glass Pattern in Go ?
  7. How to print Hollow Square Pattern in Go ?
  8. How to print Hollow Pyramid Pattern in Go ?
  9. How to print Hollow Inverted Pyramid Pattern in Go ?
  10. How to print Hollow Diamond Pattern in Go ?
  11. How to print Floyd's Trianlge Pattern in Go ?
  12. How to print Pascal's Triangle Pattern in Go ?