Go Program to Print Rectangle Number Pattern

Write a Go Program to Print Rectangle Number Pattern of 1’s. In this Golang Rectangle Number pattern example, the nested for loop iterates rectangle rows and columns and prints ones as an output.

package main

import "fmt"

func main() {

    var i, j, recRow, recCol int

    fmt.Print("Enter the Rows to Print Rectangle of 1's = ")
    fmt.Scanln(&recRow)

    fmt.Print("Enter the Columns to Print Rectangle of 1's = ")
    fmt.Scanln(&recCol)

    fmt.Println("Rectangle Pattern of 1's")
    for i = 0; i < recRow; i++ {
        for j = 0; j < recCol; j++ {
            fmt.Print("1 ")
        }
        fmt.Println()
    }
}
Go Program to Print Rectangle Number Pattern 1

Golang Program to Print Rectangle Number Pattern of 0’s

In this Rectangle Number Pattern example, we replaced 0 with 1.

package main

import "fmt"

func main() {

    var i, j, recRow, recCol int

    fmt.Print("Enter the Rows to Print Rectangle of 0's = ")
    fmt.Scanln(&recRow)

    fmt.Print("Enter the Columns to Print Rectangle of 0's = ")
    fmt.Scanln(&recCol)

    fmt.Println("Rectangle Pattern of 0's")
    for i = 0; i < recRow; i++ {
        for j = 0; j < recCol; j++ {
            fmt.Print("0 ")
        }
        fmt.Println()
    }
}
Enter the Rows to Print Rectangle of 0's = 6
Enter the Columns to Print Rectangle of 0's = 15
Rectangle 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 0 0 0 0 0 0 0 0 0 

This Go program allows entering any number and prints that number in a Rectangle pattern.

package main

import "fmt"

func main() {

    var i, j, recRow, recCol, num int

    fmt.Print("Enter the Rows to Print Rectangle of 1's = ")
    fmt.Scanln(&recRow)

    fmt.Print("Enter the Columns to Print Rectangle of 1's = ")
    fmt.Scanln(&recCol)

    fmt.Print("Enter any Number to Print as Rectangle = ")
    fmt.Scanln(&num)

    fmt.Println("Rectangle Pattern of 1's")
    for i = 0; i < recRow; i++ {
        for j = 0; j < recCol; j++ {
            fmt.Printf("%d ", num)
        }
        fmt.Println()
    }
}
Enter the Rows to Print Rectangle of 1's = 8
Enter the Columns to Print Rectangle of 1's = 22
Enter any Number to Print as Rectangle = 8
Rectangle Pattern of 1's
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.