AlgoMaster Logo

Print Numbers from 1 to N

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

Counting from 1 up to a target underlies generating row numbers, filling a range of indices, and producing a sequence to iterate over.

Given a positive integer n, return a list that starts at 1 and ends at n, with every integer in between. If n is 5, the answer is [1, 2, 3, 4, 5]. If n is 1, the answer is just [1].

A plain loop or recursion can do the counting. Both produce the same array.

Key Constraints:

  • 1 <= n <= 1000n is always at least 1, so you never have to return an empty array. The values stay small, so a plain int holds every element without any overflow concern.
  • The result is in increasing order → The numbers must run from 1 to n from left to right. Whatever method you use, the first value added has to be 1 and the last has to be n.

Approach 1: For Loop

Intuition

Start a counter at 1, add it to the result, then move the counter up by one and repeat. When the counter passes n, every number has been added and the loop ends.

The detail to get right is the loop boundary. The counter has to reach n itself, so the condition is i <= n rather than i < n. Using i < n would stop one number short and leave n out of the result.

Algorithm

  1. Create an empty list to hold the result.
  2. Start a counter i at 1.
  3. While i is less than or equal to n, add i to the list and increase i by one.
  4. Return the filled list.

Example Walkthrough

Input:

5
n

The loop starts with i = 1 and adds 1 to the list. It then adds 2, 3, and 4 on the next passes. When i reaches 5, the condition i <= 5 is still true, so 5 is added. After that i becomes 6, the condition fails, and the loop stops. The list now holds every number from 1 to 5:

0
1
1
2
2
3
3
4
4
5
result

Code

The same counting can be expressed through repeated function calls instead of a loop.

Approach 2: Recursion

Intuition

Recursion breaks the problem into one number plus a smaller version of the same task. To list the numbers from 1 to n, first list the numbers from 1 to n - 1, then add n at the end. That smaller list follows the same rule, so it lists 1 to n - 2 first and adds n - 1, and so on.

A helper carries the current value down the chain and appends it as it goes, keeping the numbers in increasing order. The base case stops the chain: once the current value passes n, there is nothing left to add, so the recursion returns.

Algorithm

  1. Create an empty list to hold the result.
  2. Define a helper that takes the current value i.
  3. If i is greater than n, stop, since every number has been added.
  4. Otherwise add i to the list, then call the helper with i + 1.
  5. Start the helper at 1 and return the filled list.

Example Walkthrough

Input:

3
n

The helper starts at i = 1, adds 1, and calls itself with 2. That call adds 2 and calls itself with 3. The next call adds 3 and calls itself with 4. Since 4 is greater than n, that call stops without adding anything, and the chain unwinds. The numbers were added in the order 1, 2, 3, so the result is in increasing order:

0
1
1
2
2
3
result

Code