Golang Program to Print Left Triangle Star Pattern


In this tutorial we will write a Go-lang code to print a Left Triangle star pattern. We will depict how you can print the star pattern in go language.

   *
  * *
 * * *
* * * *

How to print a star pattern?

A left triangle star pattern is shown above, and in this pattern, you can see that stars are increasing with the increase in the number of rows. The pattern goes like this 1 star in first row, 2 stars in second row, and goes on.

We will be using 2 for loops here, the first one will be used to print spaces and the second for loop will print * character at respective places.

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

Example 1: Golang Program to print Left Angled Star Pattern Using For Loop

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Start function main().

  • Step 3 − Declare and initialize the integer variables, (row= number rows to print).

  • Step 4 − Use the first inner for loop to iterate through the row from 1 to “number of rows”.

  • Step 5 − Use the second inner for loop to iterate through columns from 1 to i, to print a star pattern.

  • Step 5 − The outer for loop is used to count the line number.

  • Step 5 − After printing all columns of a row move to next line i.e., print new line.

Example

//GOLANG PROGRAM TO PRINT LEFT ANGLED STAR PATTERN package main // fmt package provides the function to print anything import "fmt" // this is the main function func main() { //declaring variables with integer datatype var i, j, k, row int // assigning the row variable with the value of number of rows row = 5 // Print the pattern fmt.Println("\nLeft Angled Star Pattern") // displaying the pattern using for loops for i = 0; i <= row; i++ { //printing the spaces for j = 0; j < row-i; j++ { fmt.Print(" ") } //printing the stars for k = 0; k < i; k++ { fmt.Print("*") } // printing new line fmt.Println() } }

Output

Left Angled Star Pattern 

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

Description of the Code

  • In the above code first we import the main package and the fmt package the fmt package allows us to print anything on the screen.

  • Then start the main() function.

  • Declare and initialize i, j, k and row variables of data type int.

  • Let us assign 5 to the number of rows i.e for the sake of this example let us print a star pattern with 5 number of rows.

  • I, j and k are used to iterate over the for loop. They will select the respective rows and columns for printing the * character.

  • To print this kind of pattern we need to use a for loop within another for loop. The first for loop is used to select the row, it iterates from 1 to the number that the rows contained by row variable.

  • The second for loop starts from j = 0 to the value of current row − 1.

  • The third for loop is used to print the * character.

  • Use the fmt.Print() function to print the * character inside third for loop.

  • Then print a new line.

  • In this manner the left triangle pattern will get printed.

Example 2: Golang Program to print Left Angled Star Pattern Using Recursion

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Define a printSpace() function.

  • Step 3 − Define a Star() function.

  • Step 4 − Define a pattern() function.

  • Step 5 − start the main() function.

  • Step 6 − Declare and initialize the integer variable.

  • Step 7 − Call the pattern() function.

Example

// GOLANG PROGRAM TO PRINT LEFT ANGLED STAR PATTERN USING RECURSION package main // fmt package provides the function to print anything import "fmt" // defining printSpace() function func printSpace(number int) { // defining base condition if number == 0 { return } // printing the spaces fmt.Printf(" ") // calling the printSpace() function recusrsively printSpace(number - 1) } func star(number int) { // defining base condition if number == 0 { return } // printing the * character fmt.Printf("*") // calling the star() function recursively star(number - 1) } func pattern(number, n int) { // defining base case if number == 0 { return } // calling the printSpace() function to print spaces printSpace(number - 1) // calling the star function to print * patterns star((n - number) + 1) // printing new line fmt.Println() // calling the pattern() function recursively pattern(number-1, n) } // calling the main function func main() { // declaring variable row var row int = 7 // calling the star() function pattern(row, row) }

Output

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

Description of the Code

  • In the above code first we import the main package and the fmt package the fmt package allows us to print anything on the screen.

  • Create three functions naming printSpace(), star() and pattern().

  • printSpace() function will print the spaces in the selected number of rows recursively.

  • Star() function will print * character in the respective rows recursively.

  • The third function pattern() will initiate by calling each function one by one and will print the new line.

  • Then start the main() function.

  • Initialize row variables of data type int and store number of rows that you want to print in star pattern to it.

  • Call the pattern() function and pass row variable as argument to it.

Conclusion

We have successfully compiled and executed a go language program to print a left-angled star pattern.

To print this star pattern, we have used recursion in which we are calling three functions recursively.

Updated on: 15-Nov-2022

336 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements