Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Golang Program to Print Pyramid Star Pattern
In this tutorial we will write a Go language code to print pyramid star pattern. We will depict how you can print the pyramid star pattern.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
How to print a Pyramid star pattern?
A pattern is shown above, and in the pattern, you can clearly see that 2 stars is increasing with increase in each row. The pattern goes like this 1 star in first row, 3 stars in second row, 5 starts in 3rd row and goes on.
We will use 3 for loops to print this pattern.
Example: Golang program to print pyramid star pattern
Syntax
For loop as a while loop in GO language:
for condition {
// code to be executed
// increment or decrement the count variable.
}
Algorithm
STEP 1 ? Import the package fmt
STEP 2 ? Start the function main()
STEP 3 ? Declare and initialize the integer variables, (row= number rows to print)
STEP 4 ? The first for loop iterate through the row from 1 to "row".
STEP 5 ? The second for loop iterate through columns from 1 to row-1, to print a star pattern.
STEP 6 ? The third for loop iterates from 0 to (2*i-1), and print star.
STEP 7 ? After printing all columns of a row move to next line i.e., print new line.
Example
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">//GOLANG PROGRAM TO PRINT A PYRAMID STAR PATTERN</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// calling the main function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//declaring variables with integer datatype</span>
<span class="token keyword">var</span> i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> k<span class="token punctuation">,</span> row <span class="token keyword">int</span>
<span class="token comment">// initializing row variable to a value to store number of rows</span>
row <span class="token operator">=</span> <span class="token number">5</span>
<span class="token comment">//print the pattern</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"\nThis is the pyramid pattern"</span><span class="token punctuation">)</span>
<span class="token comment">//displaying the pattern</span>
<span class="token keyword">for</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> row<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token comment">//printing the spaces</span>
<span class="token keyword">for</span> j <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> j <span class="token operator"><=</span> row<span class="token operator">-</span>i<span class="token punctuation">;</span> j<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">//printing the stars</span>
<span class="token keyword">for</span> k <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> k <span class="token operator">!=</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>i <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> k<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Print</span><span class="token punctuation">(</span><span class="token string">"*"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token comment">// printing a new line</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
This is the pyramid 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()
Next declare the integer variables that we are going to use in order to print the right pyramid star pattern in go code.
In this code the first for loop, iterates from 0 to the end of the row
The second for loop iterates from 1 to row-1, and prints empty spaces.
The third for loop iterates from 0 to (2*i-1), and prints the (*) star character.
Then we need to break the line after each row gets printed.
And finally printing the result on the screen using fmt.Printf().
Conclusion
In the above examples we have successfully compiled and executed the Golang program code to print the pyramid star pattern.
