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 Right Triangle Star Pattern
In this tutorial we will write a Go-lang code to print a right Triangle star pattern. We will depict how you can print the star pattern in go language.
* * * * * * * * * *
How to print a star pattern?
A right triangle star pattern is shown above, and in that 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.
For loop
for [condition | ( init; condition; increment) | Range] {
statement(s);
}
Example1: Golang Program To print Right Angled Star Pattern
Algorithm
Step 1 ? Import the package fmt.
Step 2 ? Start function main().
Step 3 ? Declare and initialize the integer variables.
Step 4 ? Use the first for loop to iterate through the row from 1 to "number of rows".
Step 5 ? Use the second for loop to iterate through columns from 1 to i, to print a star pattern.
Step 5 ? After printing all columns of a row move to next line i.e., print new line.
Example
The golang program to print a right triangle star pattern is compiled and executed in the following code ?
<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 RIGHT 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">// 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 to store respective values</span>
<span class="token comment">// i and j are used to iterate through for loop</span>
<span class="token comment">// row variable will contain the total number of rows that a star pattern should contain.</span>
<span class="token keyword">var</span> i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> row <span class="token keyword">int</span>
<span class="token comment">// assigning a value of 5 to the number of rows that the pattern should print.</span>
<span class="token comment">// assuming that value to be 5</span>
row <span class="token operator">=</span> <span class="token number">5</span>
<span class="token comment">// Printing the star 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">"\nRight Angled Star Pattern"</span><span class="token punctuation">)</span>
<span class="token comment">// for loop to print the number of rows rows</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">// for loop to print the number of columns</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> i<span class="token punctuation">;</span> j<span class="token operator">++</span> <span class="token punctuation">{</span>
<span class="token comment">// printing star(*) symbol using fmt.Print() function</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 using fmt.Println() function</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
Right 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 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 and j 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 = 1 to the value of current row. The value of current row is the value of i variable.
Use the fmt.Print() function to print the * character inside second for loop.
For the first row second for loop will not start and one * character will get printed.
For the second row i = 2 and in this case the second for loop will iterate from j = 1 to j = 2 in this manner in second row two * characters will get printed.
In this manner the right triangle pattern will get printed.
Example 2: Golang Program to Print Right Triangle Star pattern using Recursion
Algorithm
Step 1 ? Import the package fmt
Step 2 ? Define a printSpace() function.
Step 3 ? Define a printRow() function to print rows of star pattern recursively.
Step 4 ? Define a Star() function to print new line recursively.
Step 5 ? Declare and initialize the integer variables.
Step 6 ? Call the printRow() 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 RIGHT 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 printRow() function</span>
func <span class="token function">printRow</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">// selecting rows</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 comment">// calling the printRow() function recusrsively</span>
<span class="token function">printRow</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">// printing the star 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 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 keyword">if</span> number <span class="token operator">></span> <span class="token number">0</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>
<span class="token comment">// calling the printRow() function</span>
<span class="token function">printRow</span><span class="token punctuation">(</span>number<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 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">star</span><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 two functions naming printRow() and star().
printRow() function will print the star character in the selected number of rows.
Star() function will print new line and will call printRow() function to print * in the selected row.
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 star() function and pass row variable as argument to it.
Conclusion
We have successfully compiled and executed a go language program to print a right-angled star pattern.
To print this star pattern, we have used recursion here in which we are calling two functions recursively.
