0
0
GoProgramBeginner · 2 min read

Go Program to Print Numbers 1 to n

In Go, you can print numbers from 1 to n using a for loop like this: for i := 1; i <= n; i++ { fmt.Println(i) } where n is the limit.
📋

Examples

Input1
Output1
Input5
Output1 2 3 4 5
Input0
Output
🧠

How to Think About It

To print numbers from 1 to n, start counting from 1 and keep increasing the count by 1 until you reach n. For each number, print it on a new line. Stop when you have printed n.
📐

Algorithm

1
Get the input number n.
2
Start a counter i at 1.
3
While i is less than or equal to n, do:
4
Print the value of i.
5
Increase i by 1.
6
Stop when i becomes greater than n.
💻

Code

go
package main

import "fmt"

func main() {
    var n int
    fmt.Print("Enter a number: ")
    fmt.Scan(&n)
    for i := 1; i <= n; i++ {
        fmt.Println(i)
    }
}
Output
Enter a number: 5 1 2 3 4 5
🔍

Dry Run

Let's trace printing numbers from 1 to 3 through the code

1

Input

User enters n = 3

2

Initialize counter

Set i = 1

3

Check condition

Is i (1) <= n (3)? Yes, proceed

4

Print number

Print 1

5

Increment counter

i becomes 2

6

Repeat check

Is i (2) <= n (3)? Yes, proceed

7

Print number

Print 2

8

Increment counter

i becomes 3

9

Repeat check

Is i (3) <= n (3)? Yes, proceed

10

Print number

Print 3

11

Increment counter

i becomes 4

12

Final check

Is i (4) <= n (3)? No, stop loop

i
1
2
3
💡

Why This Works

Step 1: Input reading

The program reads the number n from the user to know how many numbers to print.

Step 2: Loop setup

A for loop starts from 1 and runs until it reaches n, controlling how many numbers to print.

Step 3: Printing numbers

Inside the loop, each number i is printed on its own line using fmt.Println(i).

🔄

Alternative Approaches

Using recursion
go
package main

import "fmt"

func printNumbers(i, n int) {
    if i > n {
        return
    }
    fmt.Println(i)
    printNumbers(i+1, n)
}

func main() {
    var n int
    fmt.Print("Enter a number: ")
    fmt.Scan(&n)
    printNumbers(1, n)
}
Recursion is elegant but uses more memory due to function calls; loops are simpler and more efficient here.
Using a while-like loop with for
go
package main

import "fmt"

func main() {
    var n int
    fmt.Print("Enter a number: ")
    fmt.Scan(&n)
    i := 1
    for i <= n {
        fmt.Println(i)
        i++
    }
}
This uses a loop that looks like a while loop, which some find easier to read.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n, so it executes n times, making the time complexity O(n).

Space Complexity

The program uses a fixed amount of extra memory regardless of n, so space complexity is O(1).

Which Approach is Fastest?

The simple for loop is fastest and most memory efficient compared to recursion, which uses extra stack space.

ApproachTimeSpaceBest For
For loopO(n)O(1)Simple and efficient printing
RecursionO(n)O(n)Elegant but uses more memory
While-like for loopO(n)O(1)Alternative loop style preference
💡
Always check that your loop condition correctly stops at n to avoid infinite loops.
⚠️
Beginners often forget to increment the counter inside the loop, causing an infinite loop.