Program to print pyramid using numbers


Example

package main

import "fmt"

func main() {
	var rows, count, count1, k int = 5, 0, 0, 0

	for i := 1; i <= rows; i++ {
		k = 0
		for space := 1; space <= rows-i; space++ {
			fmt.Print("  ")
			count++
		}
		for {
			if k == 2*i-1 {
				break
			}
			if count <= rows-1 {
				fmt.Printf("%d ", i+k)
				count++
			} else {
				count1++
				fmt.Printf("%d ", (i + k - 2*count1))
			}
			k++
		}
		count1, k, count = 0, 0, 0
		fmt.Println("")
	}
}

Output

        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Most Helpful This Week