Golang Program To Print Downward Triangle Star Pattern

In this tutorial, we will learn how to print downward triangle pattern using Go programming language.

Syntax

for initialization; condition; update {
   statement(s)
}

In the code, we use the for loop to repeat a block of code until the specified condition is met.

Example: Golang Program To Print Download Triangle Star Pattern Using One 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

<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 DOWNWARD TRIANGLE 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">// start the function main ()</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">// Declare and initialize the integer variables</span>
   <span class="token keyword">var</span> i <span class="token keyword">int</span>
   <span class="token keyword">var</span> j <span class="token keyword">int</span>
   <span class="token keyword">var</span> row <span class="token keyword">int</span> <span class="token operator">=</span> <span class="token number">9</span>
   
   <span class="token comment">// Calling Scanln () function for</span>
   <span class="token comment">// scanning and reading the input</span>
   <span class="token comment">// int given in standard input</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Scanln</span><span class="token punctuation">(</span><span class="token operator">&</span>row<span class="token punctuation">)</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">"**** Golang program to print Downward Triangle Star Pattern ****"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// using for loop to iterate through rows</span>
   <span class="token keyword">for</span> i <span class="token operator">=</span> row <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">>=</span> <span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">--</span> <span class="token punctuation">{</span>
   
      <span class="token comment">// using for loop to iterate through columns</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> i<span class="token punctuation">;</span> j<span class="token operator">++</span> <span class="token punctuation">{</span>
         <span class="token comment">// prints star</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 comment">//throws the cursor in a new line after printing each 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

**** Golang program to print Downward Triangle 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 programs. It does not take any argument nor return anything.

Declare the three integer variables i, j and row. Initialize the row variable to an integer value you want for the number of rows of the downward triangle star pattern. And call the Scanln () function for scanning and reading the input.

Using for loop ? The condition is given inside an if statement and stop execution is mentioned once the condition is right.

And last printing the result on the screen using fmt.Println.

Conclusion

We have successfully compiled and executed the Golang program code to print the downward triangle star pattern in the above example.

Updated on: 2022-11-16T06:10:26+05:30

330 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements