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

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: