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