Write a Go program to print diamond number pattern using for loop.
package main import "fmt" func main() { var i, j, k, row int fmt.Print("Enter Diamond Number Pattern Rows = ") fmt.Scanln(&row) fmt.Println("**** Diamond Number Pattern ****") for i = 1; i <= row; i++ { for j = 1; j <= row-i; j++ { fmt.Printf(" ") } for k = 1; k <= i*2-1; k++ { fmt.Printf("%d", k) } fmt.Println() } for i = row - 1; i > 0; i-- { for j = 1; j <= row-i; j++ { fmt.Printf(" ") } for k = 1; k <= i*2-1; k++ { fmt.Printf("%d", k) } fmt.Println() } }
This is another way of writing a Golang program to print diamond pattern of numbers.
package main import "fmt" func main() { var i, j, k, l, row int fmt.Print("Enter Diamond Number Pattern Rows = ") fmt.Scanln(&row) fmt.Println("**** Diamond Number Pattern ****") for i = 1; i <= row; i++ { for j = 1; j <= row-i; j++ { fmt.Printf(" ") } for k = i; k >= 1; k-- { fmt.Printf("%d", k) } for l = 2; l <= i; l++ { fmt.Printf("%d", l) } fmt.Println() } for i = row - 1; i > 0; i-- { for j = 1; j <= row-i; j++ { fmt.Printf(" ") } for k = i; k >= 1; k-- { fmt.Printf("%d", k) } for l = 2; l <= i; l++ { fmt.Printf("%d", l) } fmt.Println() } }
Enter Diamond Number Pattern Rows = 9
**** Diamond Number Pattern ****
1
212
32123
4321234
543212345
65432123456
7654321234567
876543212345678
98765432123456789
876543212345678
7654321234567
65432123456
543212345
4321234
32123
212
1