Golang Program To Print X Star Pattern

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

Syntax

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

Example: Go Program Code to Print x Star Pattern Using a Single Function

Algorithm

  • Step 1 ? Import the package fmt and strconv package.

  • Step 2 ? Start 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 X 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 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 X star pattern"</span><span class="token punctuation">)</span>

   <span class="token comment">// Declare the integer variables</span>
   <span class="token keyword">var</span> i<span class="token punctuation">,</span> a<span class="token punctuation">,</span> number<span class="token punctuation">,</span> row <span class="token keyword">int</span>

   <span class="token comment">// initialize the row variable</span>
   row <span class="token operator">=</span> <span class="token number">8</span>

   <span class="token comment">// print the X 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">"X Star Pattern"</span><span class="token punctuation">)</span>

   <span class="token comment">// Run an outer loop to iterate through rows with</span>
   <span class="token comment">// structure for (i=1; i<= number; i++) (where number = row* 2 - 1)</span>
   number <span class="token operator">=</span> row<span class="token operator">*</span><span class="token number">2</span> <span class="token operator">-</span> <span class="token number">1</span>

   <span class="token comment">// Use of For Loop</span>
   <span class="token comment">// This loop starts when i = 1</span>
   <span class="token comment">// executes till i<=number 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">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> number<span class="token punctuation">;</span> i<span class="token operator">++</span> <span class="token punctuation">{</span>
      <span class="token comment">// Since each row contains exactly row * 2 - 1 columns.</span>
      <span class="token comment">// Therefore, run inner loop as for (a=1; a<=number; a++)</span>
      <span class="token keyword">for</span> a <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> a <span class="token operator"><=</span> number<span class="token punctuation">;</span> a<span class="token operator">++</span> <span class="token punctuation">{</span>
         <span class="token comment">// For the first diagonal i.e. when row and column number both</span>
         <span class="token comment">// are equal, print star whenever if(i == a).</span>
         <span class="token comment">// For the second diagonal i.e. stars are printed if(a == number - i + 1)</span>
         <span class="token keyword">if</span> a <span class="token operator">==</span> i <span class="token operator">||</span> a <span class="token operator">==</span> number<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 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 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">// PRINT THE RESULT</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 X star pattern
X 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().

  • Declare the four integer variables i, a, number and row. Initialize the row variable to an integer value you want for the number of rows of the star pattern.

  • 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 X star pattern in the above two examples.

Updated on: 2022-11-16T06:48:53+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements