Write a Go Program to Print Box Number Pattern of 1 and 0. In this Golang Box 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, print 0’s.
package main import "fmt" func main() { var i, j, row, col int fmt.Print("Enter the Box Number Pattern Rows = ") fmt.Scanln(&row) fmt.Print("Enter the Box Number Pattern Columns = ") fmt.Scanln(&col) fmt.Println("Box Number Pattern of 1's and 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("1 ") } else { fmt.Print("0 ") } } fmt.Println() } }
Golang Program to Print Box Number Pattern of 0 and 1
In this Box Number Pattern of 1 and 0 examples, we interchanged the 0 and 1.
package main import "fmt" func main() { var i, j, row, col int fmt.Print("Enter the Box Number Pattern Rows = ") fmt.Scanln(&row) fmt.Print("Enter the Box Number Pattern Columns = ") fmt.Scanln(&col) fmt.Println("Box Number Pattern of 1's and 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("1 ") } } fmt.Println() } }
Enter the Box Number Pattern Rows = 7
Enter the Box Number Pattern Columns = 12
Box Number Pattern of 1's and 0's
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0
This Golang program allows entering the box ouside and inside values. Next, it prints the x as the box outside number, and y is the box inside numbers.
package main import "fmt" func main() { var i, j, row, col, x, y int fmt.Print("Enter the Box Number Pattern Rows and Columns = ") fmt.Scan(&row, &col) fmt.Print("Enter the Outside and Inside Values = ") fmt.Scanln(&x, &y) fmt.Println("Box Number Pattern of 1's and 0's") for i = 1; i <= row; i++ { for j = 1; j <= col; j++ { if i == 1 || i == row || j == 1 || j == col { fmt.Printf("%d ", x) } else { fmt.Printf("%d ", y) } } fmt.Println() } }
Enter the Box Number Pattern Rows and Columns = 6 20
Enter the Outside and Inside Values = 9 4
Box Number Pattern of 1's and 0's
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9