Print Numbers from 1 to N without using loop

Previous
Next

In this post, we will see how to print numbers from 1 to N without using loop.


Problem

Print number from 1 to N without using any loop.

N=10
Output: 1 2 3 4 5 6 7 8 9 10

Using Recursion

We can use tail recursion to solve this problem.

  • Base case
    • When n <= 0, return
  • call printNumbers recursively with n-1
  • Print number while returning from recursion.

Output

1 2 3 4 5 6 7 8 9 10

That’s all about how to print Numbers from 1 to N without using loop.

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog