How to print Floyd's triangle in Golang 1.20
In this video we are going to generate source code for the GoLang Program, How to print Floyd's triangle using chatGPT.
After generating we will verify and run the generated code.
IMPORTANT NOTE on SOURCE CODE: only source code that are acceptable by the youtube description box will be displayed.
In this program I have replaced the angled brackets with > or <. once you copy the source code to editor replace it with greater than or less than symbols respectively.
##############SOURCE CODE#########################
package main
import "fmt"
func main() {
var rows, num, i, j int
fmt.Print("Enter the number of rows: ")
fmt.Scan(&rows)
for i = 1; i <= rows; i++ {
for j = 1; j <= i; j++ {
fmt.Printf("%d ", num)
num++
}
fmt.Println()
}
}
Below is the explanation for the program:
This program prompts the user to enter the number of rows they want in the Floyd's Triangle. It then uses two nested for loops to print out each row of the triangle.
The outer loop iterates through each row, and the inner loop prints out the numbers in each row, incrementing the value of num each time.
When you run this program and enter a number of rows, it will print out the corresponding Floyd's Triangle. For example, if you enter 5, the program will output:
Enter the number of rows: 5
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
This is a 5-row Floyd's Triangle, with the numbers starting from 0 and incrementing by 1 for each row.
#go #goprogramming #golang #golangtutorial #golanguage