To print a pyramid pattern in Go, you can use nested loops where the outer loop controls the rows and the inner loops control the spaces and stars printed in each row.
In this example,
printPyramid
that takes an integer n
as input to represent the number of rows in the pyramid.fmt.Println()
to move to the next line.package main
import (
"fmt"
)
func printPyramid(n int) {
for i := 1; i <= n; i++ {
for j := 1; j <= n-i; j++ {
fmt.Print(" ")
}
for k := 1; k <= 2*i-1; k++ {
fmt.Print("*")
}
fmt.Println()
}
}
func main() {
printPyramid(5)
}
* *** ***** ******* *********
In this tutorial, we learned How to print Pyramid Pattern in Go language with well detailed examples.