To print Pascal's Triangle Pattern in Go, you can use nested loops where the outer loop controls the rows and the inner loop calculates the values for each row based on the binomial coefficient formula.
In this example,
n
to represent the number of rows in Pascal's triangle.pascal
to store the triangle values.package main
import "fmt"
func printPascalsTriangle(n int) {
pascal := make([][]int, n)
for i := range pascal {
pascal[i] = make([]int, i+1)
for j := range pascal[i] {
if j == 0 || j == i {
pascal[i][j] = 1
} else {
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j]
}
fmt.Printf("%d ", pascal[i][j])
}
fmt.Println()
}
}
func main() {
rows := 5
printPascalsTriangle(rows)
}
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
In this tutorial, we learned How to print Pascal's Triangle Pattern in Go language with well detailed examples.