Golang Program To Print Mirror Upper Star Triangle Pattern


In this tutorial, we will learn how to print downward triangle pattern using Go 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 Download Triangle Star 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 − Start the function main ().

  • Step 6 − Calling the function upper () to print mirror upper star triangle pattern.

  • Step 7 − Print the result using fmt.Println ().

Example

// GOLANG PROGRAM TO PRINT MIRROR UPPER STAR TRIANGLE PATTERN package main // fmt package provides the function to print anything import "fmt" // Create a function upper () func upper(row int) bool { //i for rows and j for columns //row denotes the number of rows you want to print var i int var j int row = 6 fmt.Scanln(&row) //Outer loop work for rows for i = 0; i < row; i++ { //inner loop work for space for j = row - i; j > 1; j-- { //prints space between two stars fmt.Print(" ") } //inner loop for columns for j = 0; j <= i; j++ { //prints star fmt.Print("* ") } //throws the cursor in a new line after printing each line fmt.Println() // print the result } // Outer loop fo Rows for i = 1; i <= row; i++ { // Inner loop 1 to print triangle 3 for j = 1; j < i; j++ { // Printing whitespace fmt.Print(" ") } // Inner loop 2 to print triangle 4 for j = i; j <= row; j++ { // Printing star and whitespace fmt.Print("*" + " ") } // By now done with one row so new line fmt.Println() } return true } // start the function main () func main() { fmt.Println("GOLANG PROGRAM TO PRINT MIRROR UPPER STAR TRIANGLE PATTERN") fmt.Print(upper(6)) // print the result }

Output

GOLANG PROGRAM TO PRINT MIRROR UPPER STAR TRIANGLE PATTERN
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
* * * * * * 
* * * * * * 
 * * * * * 
  * * * * 
   * * * 
    * * 
     * 
true

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.

  • Next, we create the function upper () to print the pattern.

  • 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 mirror upper star triangle pattern.

  • Using for loop  The condition is given inside an if statement and stop execution is mentioned once the condition is right.

  • Start the function main ().

  • Next, we call the function upper () to print the pattern.

  • And last printing the result on the screen using fmt.Println ().

Conclusion

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

Updated on: 16-Nov-2022

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements