Golang Program to print Floyd's triangle
Example
package main
import "fmt"
func main() {
var num = 5
lowerLeftCorner := num*(num-1)/2 + 1
lastInColumn := lowerLeftCorner
lastInRow := 1
for i, row := 1, 1; row <= num; i++ {
w := len(fmt.Sprint(lastInColumn))
if i < lastInRow {
fmt.Printf("%*d ", w, i)
lastInColumn++
} else {
fmt.Printf("%*d\n", w, i)
row++
lastInRow += row
lastInColumn = lowerLeftCorner
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Most Helpful This Week
Is There Still a Need for Programming in This AI World?
Web Application to generate QR code in Golang
Multiple-value <function> in single-value context error in Golang
Example of Fscan, Fscanf, and Fscanln from FMT Package
Example: How to use ReadAtLeast from IO Package in Golang?
GO Program to find area and circumference of circle
How to set timeout for http.Get() requests in Golang?
Golang program for implementation of AVL Trees
How do you handle HTTP timeouts with an HTTP client in Go?
Struct Instantiation Using Pointer Address Operator