Write a Go Program to Print Square Number Pattern of 1’s. In this Golang Square Number pattern example, the nested for loop iterates Square sides. Within the loop, we print ones in square shape.
package main import "fmt" func main() { var i, j, sqSide int fmt.Print("Enter Any Side of a Square to Print 1's = ") fmt.Scanln(&sqSide) fmt.Println("Square Pattern of 1's") for i = 0; i < sqSide; i++ { for j = 0; j < sqSide; j++ { fmt.Print("1 ") } fmt.Println() } }
Enter Any Side of a Square to Print 1's = 8
Square Pattern of 1's
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 1 1
1 1 1 1 1 1 1 1
Golang Program to Print Square Number Pattern of 0’s
In this Square Number Pattern example, we replaced 0 with 1.
package main import "fmt" func main() { var i, j, sqSide int fmt.Print("Enter Any Side of a Square to Print 0's = ") fmt.Scanln(&sqSide) fmt.Println("Square Pattern of 0's") for i = 0; i < sqSide; i++ { for j = 0; j < sqSide; j++ { fmt.Print("0 ") } fmt.Println() } }
Enter Any Side of a Square to Print 0's = 9
Square 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 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 Go program allows entering any number and prints that number in a square pattern.
package main import "fmt" func main() { var i, j, side, num int fmt.Print("Enter Any Side of a Square = ") fmt.Scanln(&side) fmt.Print("Number to Print as the Square Pattern = ") fmt.Scanln(&num) fmt.Println("Numeric Square Pattern") for i = 0; i < side; i++ { for j = 0; j < side; j++ { fmt.Printf("%d ", num) } fmt.Println() } }