Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Printing 1 to 1000 without loop or conditionals in C/C++
Here we will see how to print 1 to 1000 without loop or any conditional statements. As the loops cannot be used, so we can try recursions, but here another constraint that, we cannot use the conditions also. So the base case of the recursion will not be used.
Here we are solving this problem using static members. At first we are initializing the static member with 1, then in the constructor we are printing the value and increase its value. Now create an array of 1000 objects of that class, so 1000 different objects are created, so the constructor is called 1000 times. Thus we can print 1 to 1000.
Example
#includeusing namespace std; class PrintN { public: static int value; PrintN() { cout Output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, .... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000,
Advertisements
