Golang Program to print numbers with diamond pattern
Example
package main
import "fmt"
func main() {
rows := 9
space := rows / 2
num := 1
for i := 1; i <= rows; i++ {
for j := 1; j <= space; j++ {
fmt.Printf(" ")
}
count := num/2 + 1
for k := 1; k <= num; k++ {
fmt.Printf("%d", count)
if k <= num/2 {
count--
} else {
count++
}
}
fmt.Println()
if i <= rows/2 {
space = space - 1
num = num + 2
} else {
space = space + 1
num = num - 2
}
}
}
Output
1
212
32123
4321234
543212345
4321234
32123
212
1
Most Helpful This Week
How to check if an item exists in Slice in Golang?
Multiple-value <function> in single-value context error in Golang
Contains, ContainsAny, Count and EqualFold string functions in Go Language
Cannot call non-function <variable> error in Golang
How append a slice to an existing slice in Golang?
This sample program demonstrates how to decode a JSON string.
Interface Accepting Address of the Variable in Golang
Different ways for Integer to String Conversions
How do you send an HTTP GET request with an HTTP client in Go?
Go program to find MX records record of a domain