Program in Golang to print Pyramid of Numbers
Simple program in go language using for...... loop to print the Pyramid of numbers. Program first ask user to enter number of rows. Then output will be Pyramid of that rows.
Example
// Program in Golang to print Pyramid of Numbers
package main
import "fmt"
func main() {
var rows,k,temp,temp1 int
fmt.Print("Enter number of rows :")
fmt.Scan(&rows)
for i := 1; i <= rows; i++ {
for j := 1; j <= rows-i; j++ {
fmt.Print(" ")
temp++
}
for{
if( temp <= rows-1){
fmt.Printf(" %d",i+k)
temp++
}else{
temp1++
fmt.Printf(" %d",(i+k-2*temp1))
}
k++
if(k == 2*i-1){
break
}
}
temp = 0
temp1 = 0
k = 0
fmt.Println("")
}
}
Output
Enter number of rows : 5
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
Most Helpful This Week
Golang write struct to XML file
Illustration of Checkpoint Synchronization in Golang
Golang program to read XML file into struct
Example: How to use ReadFull from IO Package in Golang?
GO supports the standard arithmetic operators: (Addition, Subtraction, Multiplication, Division,Remainder)
Multiple-value <function> in single-value context error in Golang
Modernizing Legacy Applications: Critical Tips for Organizational Upgrades
Program in Go language to print Floyd's Triangle
Example of Sscan vs Sscanf vs Sscanln from FMT Package
Find odd and even numbers using goroutines and channels