Write a program to print all numbers between 1 and N without using a loop.

Method 1: Using static variable in recursive main

The idea is to call the main() function recursively, and with each call, print the next element from the series. To store information about the previous element printed, we use a static variable (Note that a global variable will also work fine).

The following C++ program demonstrates it:

OR

Method 2: Using Recursion by implementing a separate method

OR

Method 3: Using a MACRO

Method 4: Without Recursion using struct/class with static field

 
We can also use the C++ class replacing struct.


 

 
Exercise: Extend method 3 to print numbers from 1 to 1000

 
References: https://stackoverflow.com/questions/4568645/printing-1-to-1000-without-loop-or-conditionals/