How to print pyramid of numbers in Go

1 Answer

0 votes
package main

import "fmt"

func main() {
	var rows, count1, count2, n int = 5, 0, 0, 0

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

 
/*
run:
 
        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  
 
*/

 



answered Aug 13, 2020 by avibootz

Related questions

1 answer 255 views
1 answer 322 views
1 answer 116 views
1 answer 105 views
1 answer 135 views
...