Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Print 1 to 100 in C++, without loop and recursion

  • Difficulty Level : Medium
  • Last Updated : 18 Nov, 2021

Following is a C++ program that prints 1 to 100 without loop and without recursion.
 

Take a step-up from those "Hello World" programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.

CPP




#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;
    }
};
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. The concept used in this program is Template Metaprogramming
Let us see how this works. Templates in C++ allow non-datatypes also as parameter. Non-datatype means a value, not a datatype. For example, 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. In the above program, 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.
Following is another approach to print 1 to 100 without loop and recursion. 
 

CPP




#include<iostream>
using namespace std;
 
class A
{
public:
    static int a;
    A()
    {  cout<<a++<<endl;  }
};
 
int A::a = 1;
 
int main()
{
    int N = 100;
    A obj[N];
    return 0;
}

The output of this program is same as above program. 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.



Following is light weight approach to print 1 to 100 without loop and recursion with the use of goto keyword.

C++




#include<iostream>
 
int main()
{
    short sum = 0;
     
    update: sum++;
    std::cout<<sum<<std::endl;
   
      if(sum == 100) return 0;
    goto update;
}

The output of this program is same as above program. In the above program, goto keyword is jump again to the label named update. Value of  ‘sum’ is printed and incremented with every call. The program stop executing once the variable sum is equal to 100. In this way, we get all values from 1 to 100 printed on the console. 

Thanks to Lakshmanan for suggesting this approach.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!