Program in Go language to print Pascal's Triangle
Best example to identify the use of Print, Printf and Println statements. Simple program in go language using multiple for...... loops to print Pascal's Triangle.
Example
// Golang Program to Print Pascal's Triangle
package main
import "fmt"
func main(){
var rows int
var temp int = 1
fmt.Print("Enter number of rows : ")
fmt.Scan(&rows)
for i := 0; i < rows; i++ {
for j := 1; j <= rows-i ; j++ {
fmt.Print(" ")
}
for k := 0; k <= i; k++ {
if (k==0 || i==0) {
temp = 1
}else{
temp = temp*(i-k+1)/k
}
fmt.Printf(" %d",temp)
}
fmt.Println("")
}
}
Output
Enter number of rows : 7
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
Most Helpful This Week
Syntax error: unexpected <token> error in Golang
How to read names of all files and folders in current directory?
Web Application to Get Trending Hashtags Near a Location
Copy Struct Type Using Value and Pointer Reference
How to Convert string to float type in Go?
Golang write CSV records
How do you handle HTTP client caching in Go?
How do you handle HTTP server caching in Go?
Golang program for implementation of Selection Sort
How to read current directory using Readdir?