I encounter a problem when try to make a pyramid of number in reverse of golang
I already can make a pyramid of number with this code :
var (
input, bil int
)
fmt.Scanln(&input)
bil = 9
for b := 1; b <= input; b++ {
for c := input; c >= b; c-- { //spasi
fmt.Print(" ")
}
for d := 1; d <= b; d++ { //bintang
fmt.Print(bil)
if bil == -1 {
bil = 9
}
bil = bil - 1
}
fmt.Println()
}
Input :
5
Output :
9
87
654
3210
98765
How do I make a reverse one like this
input:
5
Output
9
78
456
0123
56789
Just change the way you calculate the current number (fmt.Print(bil-d)
)
func main() {
var (
input, bil int
)
fmt.Scanln(&input)
bil = 9
for b := 1; b <= input; b++ {
for c := input; c >= b; c-- {
fmt.Print(" ")
}
for d := b - 1; d >= 0; d-- {
v := bil - d
if v < 0 {
v = v%10 + 10
}
fmt.Print(v)
}
bil -= b
if bil < 0 {
bil = bil%10 + 10
}
fmt.Println()
}
}
Note that I also change the handling of boundary conditions.