
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#include<iostream> using namespace std; class PrintN { public: static int value; PrintN() { cout<< value++ <<", "; } }; int PrintN::value = 1; //initialize to 1 main() { int N = 1000; PrintN obj[N]; //create an array of size 1000 to create 1000 objects }
Output
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, .... 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000,
- Related Articles
- Printing all subsets of {1,2,3,…n} without using array or loop in C program
- Print 1 to 100 in C++, without loop and recursion
- How will you print numbers from 1 to 100 without using loop in C?
- Print a character n times without using loop, recursion or goto in C++
- Program to print numbers from 1 to 100 without using loop
- Printing Items in 0/1 Knapsack in C++
- Print a pattern without using any loop in C++
- C program to print number series without using any loop
- Fleury’s Algorithm for printing Eulerian Path or Circuit in C++
- How to find length of a string without string.h and loop in C?
- Printing Pyramid in C++
- Execute MySQL query from the terminal without printing results?
- How to print all the Armstrong Numbers from 1 to 1000 using C#?
- How to print a name multiple times without loop statement using C language?
- Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop

Advertisements