Golang Program To Print Square Star Pattern

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

Syntax

NESTED FOR LOOP:
for [condition | ( init; condition; increment ) | Range] {
   for [condition | ( init; condition; increment ) | Range] {
      statement(s);
   }
   statement(s);
}

Example: Golang Program Code To Print Square 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

<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 SQUARE 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>
<span class="token comment">// this function is the entry point of the executable program</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 the integer variable</span>
   <span class="token keyword">var</span> i<span class="token punctuation">,</span> j<span class="token punctuation">,</span> s <span class="token keyword">int</span>
   s <span class="token operator">=</span> <span class="token number">7</span>
   
   <span class="token comment">// Scanln() function scans the input, reads and stores</span>
   <span class="token comment">//the successive space-separated values into successive arguments</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>s<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 SQUARE STAR PATTERN"</span><span class="token punctuation">)</span>
   
   <span class="token comment">// Use of For Loop</span>
   <span class="token comment">// This loop starts when i = 0</span>
   <span class="token comment">// executes till i<s condition is true</span>
   <span class="token comment">// post statement is i++</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> s<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</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> s<span class="token punctuation">;</span> j<span class="token operator">++</span> <span class="token punctuation">{</span>
      
         <span class="token comment">// print *</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">// Ending line after each row</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 SQUARE 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 three integer variables i, j and s and initialize the s variable to an integer value you want for the number of rows of the square star pattern. Here, fmt.Scanln () function is used to read and store the s value.

  • Using for loop ? The condition is given inside an if statement and stop execution is mentioned once the condition is right. In the line 24 of the code: The loop starts when i = 0 and executes till i<s condition is true and the post statement is i++. In the line 25 of the code: it runs the next loop as for (j=0; j<=i; j++).

  • And finally printing the result in the form of a square with stars shape on the screen using fmt.Println () function.

Conclusion

We have successfully compiled and executed the Golang program code to print square star pattern in the above examples.

Updated on: 2022-11-16T06:32:51+05:30

730 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements