Golang Program to Print Reverse Pyramid Star Pattern

In this tutorial we will write a Go-lang code to print Reverse pyramid star pattern. We will depict how you can print the reverse pyramid star pattern.

   *********
    *******
     *****
      ***
       *

How to print a Reverse Pyramid star pattern?

A pattern is shown above, and in the pattern, you can clearly see that 2 stars is decreasing with increase in each row. If the total rows are 5, the pattern goes like these 9 stars in first row, 7 stars in second row, 5 starts in 3rd row and goes on decreasing.

We will use 4 for loops to print this pattern.

Golang program to print reverse pyramid star pattern

Syntax

for [condition | ( init; condition; increment) | Range] {
   statement(s);
}

Algorithm

  • STEP 1 ? Import the package fmt

  • STEP 2 ? Start the function main ()

  • STEP 3 ? Declare and initialize the integer variables, (row= number rows to print)

  • STEP 4 ? Use different for loops in order to print the reverse pyramid star pattern.

  • 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 REVERSE PYRAMID 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></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">//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">//initialize the number of rows to print</span>
   <span class="token keyword">var</span> rows <span class="token operator">=</span> <span class="token number">5</span>
   
   <span class="token comment">//declaring variables with integer datatype</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">"\nThis is the Reverse pyramid pattern"</span><span class="token punctuation">)</span>
   <span class="token keyword">var</span> i<span class="token punctuation">,</span> j <span class="token keyword">int</span>
   
   <span class="token comment">//displaying the pattern</span>
   <span class="token keyword">for</span> i <span class="token operator">=</span> rows<span class="token punctuation">;</span> i <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 punctuation">{</span>
      
      <span class="token comment">//printing the spaces</span>
      <span class="token keyword">for</span> space <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> space <span class="token operator"><=</span> rows<span class="token operator">-</span>i<span class="token punctuation">;</span> space<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> j <span class="token operator">=</span> i<span class="token punctuation">;</span> j <span class="token operator"><=</span> <span class="token number">2</span><span class="token operator">*</span>i<span class="token operator">-</span><span class="token number">1</span><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>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 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 operator">-</span><span class="token number">1</span><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>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>Println</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>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

This is the pyramid 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 ()

  • Next declare the integer variables that we are going to use in order to print the right pyramid star pattern in go code.

  • We have used different for loops, to print spaces and stars in order to print the reverse pyramid star pattern.

  • Break the line after each row pattern.

  • And finally printing the result on the screen using fmt.Printf().

Conclusion

In the above examples we have successfully explained and executed the Golang program code to print the Reverse pyramid star pattern.

Updated on: 2022-11-22T13:02:15+05:30

736 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements