Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Print Numbers from 1 to N

Easy Acceptance 75% Points 20

You are given an integer n. Your task is to print all the numbers starting from 1 up to n. The solution must be implemented using recursion only, and you are not allowed to use any looping constructs such as for or while.

All the numbers should be printed in increasing order, on a single line, with each number separated by a space.

Examples
Example 1

Example 1

Input: n = 15

Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Explanation: Not Needed

Example 2

Example 2

Input: n = 5

Output: 1 2 3 4 5

Explanation: Not Needed

Example 3

Example 3

Input: 2

Output: 1 2

Explanation: Not Needed

Constraints
  • 1 = n = (10^{3})
Topics
Recursion
Companies
Amazon Microsoft Atlassian
Added 07 Feb 2026 75% acceptance
Ln 1, Col 1Sign in to save your code
5
1 2 3 4 5