Write a Go program to print the sandglass star pattern using for loop.
package main import "fmt" func main() { var i, j, k, row int fmt.Print("Enter Sandglass Star Pattern Rows = ") fmt.Scanln(&row) fmt.Println("**** Sandglass Star Pattern ****") for i = 0; i <= row-1; i++ { for j = 0; j < i; j++ { fmt.Printf(" ") } for k = i; k <= row-1; k++ { fmt.Printf("* ") } fmt.Println() } for i = row - 1; i >= 0; i-- { for j = 0; j < i; j++ { fmt.Printf(" ") } for k = i; k <= row-1; k++ { fmt.Printf("* ") } fmt.Println() } }
This Go example prints the sandglass pattern of a given character.
package main import ( "bufio" "fmt" "os" ) func main() { reader := bufio.NewReader(os.Stdin) var i, j, k, row int fmt.Print("Enter Sandglass Star Pattern Rows = ") fmt.Scanln(&row) fmt.Print("Enter Character to Print in Sandglass = ") ch, _, _ := reader.ReadRune() fmt.Println("**** Sandglass Star Pattern ****") for i = 0; i <= row-1; i++ { for j = 0; j < i; j++ { fmt.Printf(" ") } for k = i; k <= row-1; k++ { fmt.Printf("%c ", ch) } fmt.Println() } for i = row - 1; i >= 0; i-- { for j = 0; j < i; j++ { fmt.Printf(" ") } for k = i; k <= row-1; k++ { fmt.Printf("%c ", ch) } fmt.Println() } }
Enter Sandglass Star Pattern Rows = 10
Enter Character to Print in Sandglass = $
**** Sandglass Star Pattern ****
$ $ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $
$ $ $ $ $ $ $
$ $ $ $ $ $
$ $ $ $ $
$ $ $ $
$ $ $
$ $
$
$
$ $
$ $ $
$ $ $ $
$ $ $ $ $
$ $ $ $ $ $
$ $ $ $ $ $ $
$ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $
$ $ $ $ $ $ $ $ $ $