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() } }
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