Golang Program To Print Mirror Lower Star Triangle Pattern


In this tutorial, we will learn how to print the mirror lower star triangle pattern using G 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 Mirror Lower Star Triangle 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

// GOLANG PROGRAM TO PRINT MIRROR LOWER STAR TRIANGLE PATTERN package main // fmt package provides the function to print anything import "fmt" func main() { fmt.Println("GOLANG PROGRAM TO PRINT MIRROR LOWER STAR TRIANGLE PATTERN") // declare the variables var i int var j int var row int // initialize the variables row = 8 //Outer loop 1 // prints the first half triangle GOLANG PROGRAM TO PRINT MIRROR LOWER STAR TRIANGLE PATTERN for i = 0; i <= row; i++ { //inner loop 1 for j = 1; j < i; j++ { //prints space between two stars fmt.Print(" ") } // Inner loop 2 for j = i; j <= row; j++ { // Print star fmt.Print("*" + " ") } // Ending line after each row fmt.Println() } // Outer loop 2 // prints the second half triangle for i = row - 1; i >= 0; i-- { // Inner loop 1 for j = 0; j < i; j++ { // Print whitespace fmt.Print(" ") } // Inner loop 2 for j = i; j <= row-1; j++ { // Print star fmt.Print("*" + " ") } // Ending line after each row fmt.Println() } }

Output

GOLANG PROGRAM TO PRINT MIRROR LOWER STAR TRIANGLE 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 three integer variables i, j and row. Initialize the row variable to an integer value you want for the number of rows of the lower mirror star triangle 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 using fmt.Println().

Conclusion

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

Updated on: 16-Nov-2022

134 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements