
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to Print Diamond Star Pattern
In this tutorial, we will learn how to print diamond star pattern using Go programming language.
Syntax
for initialization; condition; update { statement(s) }
Example: Golang Program Code To Print Diamond Star Pattern Using A Single Function
Algorithm
Step 1 ? Import the package fmt.
Step 2 ? Start the function main ().
Step 3 ? Declare and initialize the variables.
Step 4 ? Use of for loop with condition and incrementor.
Step 5 ? Print the result using fmt.Println ().
Example
// GOLANG PROGRAM TO PRINT DIAMOND STAR PATTERN package main // fmt package provides the function to print anything import "fmt" // start the function main () // this function is the entry point of the executable program func main() { // Declare the integer variables var x, y, z, rows int // initialize the rows variable rows = 8 fmt.Scanln(&rows) fmt.Println("GOLANG PROGRAM TO PRINT DIAMOND STAR PATTERN") // Use of For Loop for the upper half for x = 1; x <= rows; x++ { // This loop starts when y = 1 // executes till y<=rows condition is true // post statement is y++ for y = 1; y <= rows-x; y++ { fmt.Printf(" ") } for z = 1; z <= x*2-1; z++ { fmt.Printf("*") } fmt.Println() } // Use of For Loop for the lower half for x = rows - 1; x > 0; x-- { // This loop starts when y = 1 // executes till y<=rows condition is true // post statement is y++ for y = 1; y <= rows-x; y++ { fmt.Printf(" ") } for z = 1; z <= x*2-1; z++ { fmt.Printf("*") } fmt.Println() // print the result using fmt.Println () function } }
Output
GOLANG PROGRAM TO PRINT DIAMOND STAR PATTERN * *** ***** ******* ********* *********** ************* *************** ************* *********** ********* ******* ***** *** *
Description Of The Code
In the above program, we first declare the package main.
We imported the fmt package that includes the files of package fmt.
Now start the function main () and this function is the entry point of the executable program. It does not take any argument nor return anything.
Declare the four integer variables x, y, z and rows. Initialize the rows variable to an integer value you want for the number of rows of the diamond star pattern. Here, fmt.Scanln () function is used to read and store the rows value.
Using for loop ? The condition is given inside an if statement and stop execution is mentioned once the condition is right. In the code the for loop starts when x := rows - 1 ,y := 1 and i = 1.
And finally printing the result in the form of a diamond shape on the screen using fmt.Println () function which formats using the default formats for its operands and writes to standard output.
Conclusion
We have successfully compiled and executed the Golang program code to print diamond star pattern in the above example.