This Go program to print natural numbers in reverse order uses the for loop to start traverse from n until 1. Within the loop, the println statement prints natural numbers.
package main import "fmt" func main() { var num, i int fmt.Print("\nEnter the Last Natural Number = ") fmt.Scanln(&num) fmt.Println("\nNatural Numbers from ", num, " to 1 are = ") for i = num; i >= 1; i = i - 1 { fmt.Print(i, "\t") } fmt.Println() }
Enter the Last Natural Number = 10
Natural Numbers from 10 to 1 are =
10 9 8 7 6 5 4 3 2 1
Golang Program to Print Natural Numbers in Reverse
In this Golang program, we alter the for loop.
package main import "fmt" func main() { var num int fmt.Print("\nEnter the Last Natural Number = ") fmt.Scanln(&num) fmt.Println("\nNatural Numbers from ", num, " to 1 are = ") for num >= 1 { fmt.Print(num, "\t") num = num - 1 } fmt.Println() }
Enter the Last Natural Number = 20
Natural Numbers from 20 to 1 are =
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
This Go program accepts the minimum and maximum values and prints the natural numbers from maximum to minimum limit.
package main import "fmt" func main() { var minnum, maxnum int fmt.Print("\nEnter the First Natural Number = ") fmt.Scanln(&minnum) fmt.Print("\nEnter the Last Natural Number = ") fmt.Scanln(&maxnum) fmt.Println("\nNatural Numbers from ", minnum, " to ", maxnum, " are = ") for maxnum >= minnum { fmt.Print(maxnum, "\t") maxnum = maxnum - 1 } fmt.Println() }