Write a Go Program to Print Hollow Box Number Pattern of 1’s. In this Golang Hollow Box Number pattern example, the nested for loop iterates rows and columns. The if statement (if i == 1 || i == row || j == 1 || j == col) checks whether it is a first row or first column or last row or last column. If true, print 1’s; otherwise, it prints empty space.
package main
import "fmt"
func main() {
var i, j, row, col int
fmt.Print("Enter the Hollow Box Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Println("Hollow Box Number Pattern")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Print("1 ")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
Enter the Hollow Box Rows and Columns = 7 22
Hollow Box Number Pattern
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
1 1
1 1
1 1
1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Golang Program to Print Hollow Box Number Pattern of 0’s
In this Hollow Box Number Pattern example, we replaced 0 with 1.
package main
import "fmt"
func main() {
var i, j, row, col int
fmt.Print("Enter the Hollow Box of 0's Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Println("Hollow Box Number Pattern of 0's")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Print("0 ")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
Enter the Hollow Box of 0's Rows and Columns = 8 20
Hollow Box Number Pattern of 0's
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
This Golang example allows entering the number and prints that number in a hollow box pattern.
package main
import "fmt"
func main() {
var i, j, row, col, num int
fmt.Print("Enter the Hollow Box Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Print("Enter the Number to Print Hollow Box = ")
fmt.Scanln(&num)
fmt.Println("Hollow Box Number Pattern")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Printf("%d ", num)
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
