In the previous article, we have discussed about Print all Integers that Aren’t Divisible by Either 2 or 3 and Lie between 1 and 50 in C++ and Python. Let us learn About Object-Oriented Approach to Display a Sequence of Numbers without any for-Loop or Recursion in C++ Program.
Displaying a Sequence of Numbers Without any For-Loop or Recursion
In this article we will we will discuss about how to display a sequence of numbers without any For-Loop or Recursion.
Suppose we want to display from 15 to 25 without using any For-Loop or Recursion. i.e.
15, 16, 17, 18,19, 20, 21, 22, 23, 24, 25
So we will two concepts to achieve this. i.e.
- Constructor
- Static variable
C++ program to display a sequence of numbers without any For-Loop or Recursion :
// Program : #include <iostream> struct DisplayNumber { // static member variable created i.e. Count static int Count; // constructore DisplayNumber() { // Printing Count value std::cout<<Count<<std::endl; // incrementing the Count value Count++; } }; // Starting value of Count int DisplayNumber::Count = 15; int main() { // 11 objects created, so constructor will be called 10 times. DisplayNumber objArr[11]; return 0; }
Output : 15 16 17 18 19 20 21 22 23 24 25