Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Print Numbers from 1 to N

Easy Acceptance 75.00% Points 20.00

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

Hints
Hint 1
N/A
Constraints
  • 1 = n = (10^{3})
Companies
Amazon Microsoft Atlassian
Topics
Recursion
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions