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