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 Left Triangle Star Pattern
In this tutorial we will write a Go-lang code to print a Left Triangle star pattern. We will depict how you can print the star pattern in go language.
* * * * * * * * * *
How to print a star pattern?
A left triangle star pattern is shown above, and in this pattern, you can see that stars are increasing with the increase in the number of rows. The pattern goes like this 1 star in first row, 2 stars in second row, and goes on.
We will be using 2 for loops here, the first one will be used to print spaces and the second for loop will print * character at respective places.
For loop:
for [condition | ( init; condition; increment) | Range] {
statement(s);
}
Example 1: Golang Program to print Left Angled Star Pattern Using For Loop
Algorithm
Step 1 ? Import the package fmt.
Step 2 ? Start function main().
Step 3 ? Declare and initialize the integer variables, (row= number rows to print).
Step 4 ? Use the first inner for loop to iterate through the row from 1 to "number of rows".
Step 5 ? Use the second inner for loop to iterate through columns from 1 to i, to print a star pattern.
Step 5 ? The outer for loop is used to count the line number.
Step 5 ? 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 LEFT ANGLED 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">// this is 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">// assigning the row variable with the value of 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">"\nLeft Angled Star Pattern"</span><span class="token punctuation">)</span>
<span class="token comment">// displaying the pattern using for loops</span>
<span class="token keyword">for</span> i <span class="token operator">=</span> <span class="token number">0</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">0</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> i<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 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
Left Angled Star Pattern
*
**
***
****
*****
Description of the Code
In the above code first we import the main package and the fmt package the fmt package allows us to print anything on the screen.
Then start the main() function.
Declare and initialize i, j, k and row variables of data type int.
Let us assign 5 to the number of rows i.e for the sake of this example let us print a star pattern with 5 number of rows.
I, j and k are used to iterate over the for loop. They will select the respective rows and columns for printing the * character.
To print this kind of pattern we need to use a for loop within another for loop. The first for loop is used to select the row, it iterates from 1 to the number that the rows contained by row variable.
The second for loop starts from j = 0 to the value of current row ? 1.
The third for loop is used to print the * character.
Use the fmt.Print() function to print the * character inside third for loop.
Then print a new line.
In this manner the left triangle pattern will get printed.
Example 2: Golang Program to print Left Angled Star Pattern Using Recursion
Algorithm
Step 1 ? Import the package fmt
Step 2 ? Define a printSpace() function.
Step 3 ? Define a Star() function.
Step 4 ? Define a pattern() function.
Step 5 ? start the main() function.
Step 6 ? Declare and initialize the integer variable.
Step 7 ? Call the pattern() function.
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 LEFT ANGLED STAR PATTERN USING RECURSION</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">// defining printSpace() function</span>
func <span class="token function">printSpace</span><span class="token punctuation">(</span>number <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// defining base condition</span>
<span class="token keyword">if</span> number <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token comment">// printing the spaces</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">" "</span><span class="token punctuation">)</span>
<span class="token comment">// calling the printSpace() function recusrsively</span>
<span class="token function">printSpace</span><span class="token punctuation">(</span>number <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">star</span><span class="token punctuation">(</span>number <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// defining base condition</span>
<span class="token keyword">if</span> number <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token comment">// printing the * character</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"*"</span><span class="token punctuation">)</span>
<span class="token comment">// calling the star() function recursively</span>
<span class="token function">star</span><span class="token punctuation">(</span>number <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
func <span class="token function">pattern</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> n <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// defining base case</span>
<span class="token keyword">if</span> number <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span>
<span class="token punctuation">}</span>
<span class="token comment">// calling the printSpace() function to print spaces</span>
<span class="token function">printSpace</span><span class="token punctuation">(</span>number <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment">// calling the star function to print * patterns</span>
<span class="token function">star</span><span class="token punctuation">(</span><span class="token punctuation">(</span>n <span class="token operator">-</span> number<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span>
<span class="token comment">// printing 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 comment">// calling the pattern() function recursively</span>
<span class="token function">pattern</span><span class="token punctuation">(</span>number<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> n<span class="token punctuation">)</span>
<span class="token punctuation">}</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 variable row</span>
<span class="token keyword">var</span> row <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">7</span>
<span class="token comment">// calling the star() function</span>
<span class="token function">pattern</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span> row<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
*
**
***
****
*****
******
*******
Description of the Code
In the above code first we import the main package and the fmt package the fmt package allows us to print anything on the screen.
Create three functions naming printSpace(), star() and pattern().
printSpace() function will print the spaces in the selected number of rows recursively.
Star() function will print * character in the respective rows recursively.
The third function pattern() will initiate by calling each function one by one and will print the new line.
Then start the main() function.
Initialize row variables of data type int and store number of rows that you want to print in star pattern to it.
Call the pattern() function and pass row variable as argument to it.
Conclusion
We have successfully compiled and executed a go language program to print a left-angled star pattern.
To print this star pattern, we have used recursion in which we are calling three functions recursively.
