# Golang Program To Create Pyramid And Pattern

In this tutorial, we will learn how to create pyramids with different patterns in Go programming language

### Syntax

NESTED FOR LOOP
for [condition | ( init; condition; increment ) | Range] {
for [condition | ( init; condition; increment ) | Range] {
statement(s);
}
statement(s);
}


## Example 1: Golang code to Create Pyramid And Pattern Using $### Algorithm • Step 1 − Import the package fmt. • Step 2 − Start 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 package main // fmt package provides the function to print anything import "fmt" func main() { // declare the integer variables var i, j, rows int rows = 7 // Scanln() reads data from rows and stores it in the places // specified by the additional arguments in the parameter format fmt.Scanln(&rows) fmt.Println("\nGolang program to create pyramid and pattern") for i = 1; i <= rows; i++ { for j = 1; j <= rows-i; j++ { fmt.Print(" ") } for k := 0; k != (2*i - 1); k++ { fmt.Print("$")
}
fmt.Println()
}
}


### Output

Golang program to create pyramid and pattern
$$
$$


### Description of the Code

• In the above program, we first declare the package main.

• We have 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 three integer variables i, j and rows. Initialize the rows variable to an integer value you want for pyramid of $pattern pattern. Use fmt.Scanln () to read and store the rows value. • 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 in the form of a pyramid with$ pattern on the screen using fmt.Println () function.

## Example 2: Golang Code to Create a Pyramid with Number Pattern

### Algorithm

• Step 1 − Import the package fmt and package strconv.

• Step 2 − Start function main ().

• Step 3 − Declare and initialize the variable rows.

• Step 4 − Initialize k=0, NUM1=0, rows=5, NUM2=0.

• Step 5 − Use of for loop with condition and incrementor.

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

### Example

package main
// fmt package provides the function to print anything
import "fmt"
func main() {

// DECLARE AND INITIALIZE THE INTEGER VARIABLES
var rows, NUM1, NUM2, k int = 5, 0, 0, 0

// USING FOR LOOP
for i := 1; i <= rows; i++ {
k = 0
for space := 1; space <= rows-i; space++ {
fmt.Print(" ")
NUM1++
}
for {
if k == 2*i-1 {
break
}
if NUM1 <= rows-1 {
fmt.Printf("%d ", i+k)
NUM1++
} else {
NUM2++
fmt.Printf("%d ", (i + k - 2*NUM2))
}
k++
}
NUM2, k, NUM1 = 0, 0, 0
fmt.Println("")
}
// PRINT THE PYRAMID WITH NUMBER PATTERN
}


### Output

    1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5


### 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 NUM1, NUM2, k and rows. Initialize the rows variable to an integer value you want for pyramid of numbers pattern. Use fmt.Scanln () to read and store the rows value.

• 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 in the form of a pyramid with numbers pattern on the screen using fmt.Println () function.

## Example 3: Golang Program code to Create a pyramid Using Alphabets Pattern

### Algorithm

• Step 1 − Import the package fmt and package strconv.

• Step 2 − Start 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

package main
// fmt package provides the function to print anything
import "fmt"
func main() {

// declare and initialize the integer variable
var rows int
rows = 7
fmt.Scanf("%d", &rows)
fmt.Printf("Golang program to create pyramid and pattern\n")
alphabet := 64
i := 1

// using for loop
for i <= rows {
j := 1
for j <= rows-i {
fmt.Printf(" ")
j++
}
k := i
for 0 < k {
fmt.Printf("%c", alphabet+k)
k--
}
l := 2
for l <= i {
fmt.Printf("%c", alphabet+l)
l++
}
fmt.Printf("\n")
i++
}
// print the result
}


### Output

Golang program to create pyramid and pattern
A
BAB
CBABC
DCBABCD
EDCBABCDE
FEDCBABCDEF
GFEDCBABCDEFG


### 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 variable rows and initialize the variable to an integer value you want for pyramid of alphabets pattern. Use fmt.Scanln () to read and store the rows value.

• 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 in the form of a pyramid with alphabet pattern on the screen using fmt.Println () function.

## Conclusion

We have successfully compiled and executed the Golang program code to create pyramid with patterns of \$, numbers and alphabet patterns using for loop in the above three examples.

Updated on: 16-Nov-2022

1K+ Views