Suppose we have to display a sequence of numbers like from 10 to 20 i.e.

10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

without using any for-loop and recursion.

Solution:

We will use two concepts here,

  • Constructor
  • Static Variable

Create a Structure with a static member variable i.e. a Counter. Then in this structure’s constructor display the Counter and increment it.

 

Now in the main function just Create 11 Objects of this Structure and hence constructor will be called 10 times.
Inside each constructor call, a static counter will be printed and its value will be incremented.

Thus a sequence of integers will be printed.

 

Python Resources

C++11 / C++14 Resources

Design Patterns Resources

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,