Print 1 to 100 in C++ Without Loops and Recursion
Last Updated :
21 Jun, 2022
We can print 1 to 100 without using loops and recursion using three approaches discussed below:
1) Template Metaprogramming: Templates in C++ allow non-datatypes also as parameters. Non-datatype means a value, not a datatype.
Example:
CPP
// CPP Program to print 1 to 100
// without loops and recursion
#include <iostream>
using namespace std;
template <int N> class PrintOneToN {
public:
static void print()
{
PrintOneToN<N - 1>::print();
// Note that this is not recursion
cout << N << endl;
}
};
template <> class PrintOneToN<1> {
public:
static void print() { cout << 1 << endl; }
};
// Driver Code
int main()
{
const int N = 100;
PrintOneToN<N>::print();
return 0;
}
Output
1
2
3
..
..
98
99
100
The program prints all numbers from 1 to n without using a loop and recursion.
Explanation:
- In the above program, N is passed as a value which is not a datatype. A new instance of a generic class is created for every parameter and these classes are created at compile time.
- Here, when compiler sees the statement "PrintOneToN<>::print()" with N = 100, it creates an instance PrintOneToN<100>.
- In function PrintOneToN<100>::print(), another function PrintOneToN<99>::print() is called, therefore an instance PrintOneToN<99> is created.
- Similarly, all instances from PrintOneToN<100> to PrintOneToN<2> are created. PrintOneToN<1>::print() is already there and prints 1.
- The function PrintOneToN<2> prints 2 and so on. Therefore we get all numbers from 1 to N printed on the screen.
2) Following is another approach using classes and static variables,
CPP
// CPP Program to print 1 to 100 without loops and recursion
#include <iostream>
using namespace std;
class A {
public:
static int a;
A() { cout << a++ << endl; }
};
int A::a = 1;
// Driver Code
int main()
{
int N = 100;
A obj[N];
return 0;
}
Output
1
2
3
..
..
98
99
100
The output of this program is the same as the above program.
Explanation: In the above program, class A has a static variable 'a', which is incremented with every instance of A. The default constructor of class A prints the value of 'a'. When we create an array of objects of type A, the default constructor is called for all objects one by one. Therefore, we get all values from 1 to 100 printed on the screen.
3) Using goto keyword: The goto statement is a jump statement and can be used to jump from anywhere to anywhere within a function.
Example:
C++
// CPP Program to print 1 to 100
// without loops and recursion
#include <iostream>
// Driver Code
int main()
{
short sum = 0;
update:
sum++;
std::cout << sum << std::endl;
if (sum == 100)
return 0;
goto update;
}
Output
1
2
3
..
..
98
99
100
The output of this program is the same as the above program.
Explanation: In the above program, goto keyword is jump again to the label named update. The value of 'sum' is printed and incremented with every call. The program stops executing once the variable sum is equal to 100. In this way, we get all values from 1 to 100 printed on the console.
Time complexity : O(1)
Auxiliary Space : O(1)
Similar Reads
Print 1 to 100 without loop using Goto and Recursive-main Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the
5 min read
Print a number 100 times without using loop, recursion and macro expansion in C? It is possible to solve this problem using loop or a recursion method. And we have already seen the solution using #define directive (Macro expansion) but what if all three are not allowed? A simple solution is to write the number 100 times in cout statement. A better solution is to use concept of C
1 min read
Print a character n times without using loop, recursion or goto in C++ Given a character c and a number n, print the character c, n times. We are not allowed to use loop, recursion, and goto. Examples : Input : n = 10, c = 'a'Output : aaaaaaaaaa Input : n = 6, character = '@'Output : @@@@@@ In C++, there is a way to initialize a string with a value. It can be used to p
2 min read
How will you print numbers from 1 to 100 without using a loop? If we take a look at this problem carefully, we can see that the idea of "loop" is to track some counter value, e.g., "i = 0" till "i <= 100". So, if we aren't allowed to use loops, how can we track something in the C language?Well, one possibility is the use of 'recursion', provided we use the t
12 min read
How to Read and Print an Integer value in C++ The given task is to take an integer as input from the user and print that integer in C++ language. In this article, we will learn how to read and print an integer value. In the below program, the syntax and procedures to take the integer as input from the user is shown in C++ language. Standard Inp
2 min read
Print individual digits as words without using if or switch Given a number, print words for individual digits. It is not allowed to use if or switch.Examples: Input: n = 123 Output: One Two Three Input: n = 350 Output: Three Five Zero We strongly recommend you to minimize your browser and try this yourself first. The idea is to use an array of strings to sto
5 min read