Go Program to Print X Star Pattern

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()
	}
}
Go Program to Print X Star Pattern

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
$                  $ 
 $                $  
  $              $   
   $            $    
    $          $     
     $        $      
      $      $       
       $    $        
        $  $         
         $          
        $  $         
       $    $        
      $      $       
     $        $      
    $          $     
   $            $    
  $              $   
 $                $  
$                  $

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.