Golang Program to Print Half Diamond Star Pattern

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

Syntax

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

To use a for loop in go lang we first need to initialize a variable then specify the condition of the loop i.e when should the loop end then we need to increment or decrement the variable followed by the lines of codes that should get executed.

Example 1: Golang Program Code to print Half Diamond Star Pattern Using a Single Function

Approach

The goal is to divide the pattern into an upper and a bottom half. Then, using loops, print each pattern independently. The main finding for printing the upper and lower halves is as follows ?

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? Start function main().

  • Step 3 ? Declare and initialize the variables.

  • Step 4 ? Use the for loop with to print the half diamond star pattern.

  • 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 DIAMOND 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 variables</span>
   <span class="token keyword">var</span> i<span class="token punctuation">,</span> j <span class="token keyword">int</span>
   
   <span class="token comment">// initialize the rows variable</span>
   column <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">8</span>
   count <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span>
   
   <span class="token comment">// initialize the rows variable</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 HALF DIAMOND STAR PATTERN"</span><span class="token punctuation">)</span>
   <span class="token comment">//THIS LOOP IS TO PRINT THE NUMBER OF STAR FOR COLUMNS</span>
   i <span class="token operator">=</span> <span class="token number">1</span>
   <span class="token keyword">for</span> i <span class="token operator"><</span> column <span class="token operator">*</span> <span class="token number">2</span> <span class="token punctuation">{</span>
      j <span class="token operator">=</span> <span class="token number">1</span>
      <span class="token keyword">for</span> j <span class="token operator"><=</span> count <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>
         j<span class="token operator">++</span>
      <span class="token punctuation">}</span>
      <span class="token comment">// CONDITIONAL STATEMENT FOR PRINTING * FOR HALF DIAMOND</span>
      <span class="token keyword">if</span> i <span class="token operator"><</span> column <span class="token punctuation">{</span>
         count<span class="token operator">++</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
         count<span class="token operator">--</span>
      <span class="token punctuation">}</span>
      <span class="token comment">// TO BREAK THE LINE</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">"\n"</span><span class="token punctuation">)</span>
      i<span class="token operator">++</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 HALF DIAMOND 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 four integer variables i, j, N and column and assign appropriate values to them. Initialize the N variable to an integer value you want for the number of rows of the diamond star pattern. Here, fmt.Scanln () function is used to read and store the rows value.

  • Using for loop to print the required pattern. The condition is given inside an if statement and increase the "count" if the condition is right.

  • And finally printing the result in the form of a Half diamond shape on the screen using fmt.Println() function which formats using the default formats for its operands and writes to standard output.

Example 2: Golang Program code to Print Half Diamond Star Pattern in two Separate Functions

Algorithm

  • Step 1 ? Import the package fmt.

  • Step 2 ? Create the function halfdiamond().

  • Step 3 ? Declare and initialize the variables.

  • Step 4 ? Use of for loop with condition and incrementor.

  • Step 5 ? Start the function main().

  • Step 6 ? Calling the function halfdiamond().

  • Step 7 ? 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 DIAMOND 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">// create a function for the diamond star pattern</span>
func <span class="token function">halfdiamond</span><span class="token punctuation">(</span>column <span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// initialize the counter</span>
   count <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span>
   
   <span class="token comment">// THIS LOOP IS TO PRINT THE NUMBER OF STAR FOR COLUMNS</span>
   i <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span>
   <span class="token keyword">for</span> i <span class="token operator"><</span> column<span class="token operator">*</span><span class="token number">2</span> <span class="token punctuation">{</span>
      j <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1</span>
      <span class="token keyword">for</span> j <span class="token operator"><=</span> count <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>
         j<span class="token operator">++</span>
      <span class="token punctuation">}</span>
      <span class="token comment">// CONDITIONAL STATEMENT FOR PRINTING * FOR HALF DIAMOND</span>
      <span class="token keyword">if</span> i <span class="token operator"><</span> column <span class="token punctuation">{</span>
         count<span class="token operator">++</span>
      <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
         count<span class="token operator">--</span>
      <span class="token punctuation">}</span>
      <span class="token comment">// TO BREAK THE LINE</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">"\n"</span><span class="token punctuation">)</span>
      i<span class="token operator">++</span>
   <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// start 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 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 DIAMOND STAR PATTERN\n"</span><span class="token punctuation">)</span>
   <span class="token comment">// calling the function halfdiamond() and passing the number of rows to be printed as an argument</span>
   <span class="token comment">// to the function.</span>
   <span class="token function">halfdiamond</span><span class="token punctuation">(</span><span class="token number">8</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 HALF DIAMOND 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 create a function halfdiamond() that contains the logic to print only the half pattern of a diamond star pattern

  • Declare the four integer variables i, j, count and column and assign appropriate values to them. Initialize the column variable with an integer value. Here, fmt.Scanln() function is used to read and store the rows value.

  • Now the for loop is used, in order to print the required pattern. A condition is given inside an if statement which stops execution if mentioned condition is right.

  • Next, start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Under main(), the function halfdiamond () function is called for the pattern and the number of rows that star pattern should carry is passed as an argument to it.

  • Finally, the result in the form of a diamond star is printed on the screen using fmt.Println () function which formats using the default formats for its operands and writes to standard output.

Conclusion

We have successfully compiled and executed the Golang program code to printHalf diamond star pattern in the above two examples.

We have used two approaches here one is by using the main() function and also by calling the different function.

Updated on: 2022-11-15T11:32:13+05:30

433 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements