This Go program to print natural numbers from 1 to N uses the for loop to iterate values from 1 to user-entered value. Within the loop, the println statement prints each number as an output.
package main import "fmt" func main() { var num, i int fmt.Print("\nEnter the Maximum Natural Number Limit = ") fmt.Scanln(&num) fmt.Println("\nNatural Numbers from 1 to ", num, " are = ") for i = 1; i <= num; i++ { fmt.Print(i, "\t") } fmt.Println() }
Enter the Maximum Natural Number Limit = 30
Natural Numbers from 1 to 30 are =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
Golang Program to Print Natural Numbers
In this Golang program, we allow the user to enter the minimum and maximum limit and print the natural numbers between them.
package main import "fmt" func main() { var stnum, ennum int fmt.Print("\nEnter the Starting Natural Number = ") fmt.Scanln(&stnum) fmt.Print("\nEnter the Maximum Natural Number Limit = ") fmt.Scanln(&ennum) fmt.Println("\nNatural Numbers from ", stnum, " to ", ennum, " are = ") for stnum <= ennum { fmt.Print(stnum, "\t") stnum++ } fmt.Println() }