Go Program to Print Right Triangle of 1 and 0

Write a Go program to print the right angled triangle of 1 and 0’s using for loop.

package main

import "fmt"

func main() {

	var i, j, row int

	fmt.Print("Enter Right Triangle of 1 & 0 Rows = ")
	fmt.Scanln(&row)

	fmt.Println("Right Triangle of 1 and 0 Pattern")

	for i = 1; i <= row; i++ {
		for j = 1; j <= i; j++ {
			if j%2 == 0 {
				fmt.Printf("0 ")
			} else {
				fmt.Printf("1 ")
			}
		}
		fmt.Println()
	}
}
Go Program to Print Right Triangle of 1 and 0

This Go example prints the right angled triangle of 0 and 1’s as alternative columns. For this, we replaced the 0 with 1.

package main

import "fmt"

func main() {

	var i, j, row int

	fmt.Print("Enter Right Triangle of 1 & 0 Rows = ")
	fmt.Scanln(&row)

	fmt.Println("Right Triangle of 1 and 0 Pattern")

	for i = 1; i <= row; i++ {
		for j = 1; j <= i; j++ {
			if j%2 == 0 {
				fmt.Printf("1 ")
			} else {
				fmt.Printf("0 ")
			}
		}
		fmt.Println()
	}
}
Enter Right Triangle of 1 & 0 Rows = 12
Right Triangle of 1 and 0 Pattern
0 
0 1 
0 1 0 
0 1 0 1 
0 1 0 1 0 
0 1 0 1 0 1 
0 1 0 1 0 1 0 
0 1 0 1 0 1 0 1 
0 1 0 1 0 1 0 1 0 
0 1 0 1 0 1 0 1 0 1 
0 1 0 1 0 1 0 1 0 1 0 
0 1 0 1 0 1 0 1 0 1 0 1 

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.