Golang Program to Print Right Triangle Star Pattern


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

*
* *
* * *
* * * *

How to print a star pattern?

A right triangle star pattern is shown above, and in that 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.

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

Example1: Golang Program To print Right Angled Star Pattern

Algorithm

  • Step 1 − Import the package fmt.

  • Step 2 − Start function main().

  • Step 3 − Declare and initialize the integer variables.

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

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

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

Example

The golang program to print a right triangle star pattern is compiled and executed in the following code −

//GOLANG PROGRAM TO PRINT RIGHT ANGLED STAR PATTERN package main // fmt package provides the function to print anything import "fmt" // calling the main function func main() { // declaring variables with integer datatype to store respective values // i and j are used to iterate through for loop // row variable will contain the total number of rows that a star pattern should contain. var i, j, row int // assigning a value of 5 to the number of rows that the pattern should print. // assuming that value to be 5 row = 5 // Printing the star pattern fmt.Println("\nRight Angled Star Pattern") // for loop to print the number of rows rows for i = 1; i <= row; i++ { // for loop to print the number of columns for j = 1; j <= i; j++ { // printing star(*) symbol using fmt.Print() function fmt.Print("* ") } // printing a new line using fmt.Println() function fmt.Println() } }

Output

Right 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 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 and j 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 = 1 to the value of current row. The value of current row is the value of i variable.

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

  • For the first row second for loop will not start and one * character will get printed.

  • For the second row i = 2 and in this case the second for loop will iterate from j = 1 to j = 2 in this manner in second row two * characters will get printed.

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

Example 2: Golang Program to Print Right Triangle Star pattern using Recursion

Algorithm

  • Step 1 − Import the package fmt

  • Step 2 − Define a printSpace() function.

  • Step 3 − Define a printRow() function to print rows of star pattern recursively.

  • Step 4 − Define a Star() function to print new line recursively.

  • Step 5 − Declare and initialize the integer variables.

  • Step 6 − Call the printRow() function.

Example

//GOLANG PROGRAM TO PRINT RIGHT ANGLED STAR PATTERN using recursion package main // fmt package provides the function to print anything import "fmt" // defining printRow() function func printRow(number int) { // selecting rows if number > 0 { // calling the printRow() function recusrsively printRow(number - 1) // printing the star character fmt.Printf("*") } } func star(number int) { if number > 0 { // calling the star function recursively star(number - 1) } // calling the printRow() function printRow(number) // printing new line fmt.Println() } // calling the main function func main() { // declaring variable row var row int = 7 // calling the star() function star(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 two functions naming printRow() and star().

  • printRow() function will print the star character in the selected number of rows.

  • Star() function will print new line and will call printRow() function to print * in the selected row.

  • 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 star() function and pass row variable as argument to it.

Conclusion

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

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

Updated on: 15-Nov-2022

606 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements