Print Numbers from 1 to N without using loop

Objective: Given a number N, write an program to print from number 1 to N without using loop.

Example:

N = 20
Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Approach: Use Recursion

  • Make recursive to N = N-1.
  • In tail recursion, print the number

Code:

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: