Go Program to Print Same Numbers on all Sides of a Square

Write a Go program to print the same numbers on all sides of a square pattern using for loop.

package main

import "fmt"

func main() {

	var i, j, k, row int

	fmt.Print("Enter Sqaure With All Sides Same Number Rows = ")
	fmt.Scanln(&row)

	fmt.Println("Same Number on All Sides of  a Square Pattern")

	for i = 1; i <= row; i++ {
		for j = 1; j <= row; j++ {
			if i < j {
				fmt.Printf("%d ", row-i+1)
			} else {
				fmt.Printf("%d ", row-j+1)
			}
		}
		for k = row - 1; k >= 1; k-- {
			if i < k {
				fmt.Printf("%d ", row-i+1)
			} else {
				fmt.Printf("%d ", row-k+1)
			}
		}
		fmt.Println()
	}

	for i = row - 1; i > 1; i-- {
		for j = 1; j <= row; j++ {
			if i < j {
				fmt.Printf("%d ", row-i+1)
			} else {
				fmt.Printf("%d ", row-j+1)
			}
		}
		for k = row - 1; k >= 1; k-- {
			if i < k {
				fmt.Printf("%d ", row-i+1)
			} else {
				fmt.Printf("%d ", row-k+1)
			}
		}
		fmt.Println()
	}
}
Go Program to Print Same Numbers on all Sides of a Square

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.