Skip to content
Related Articles

Related Articles

Improve Article
Print a number 100 times without using loop, recursion and macro expansion in C?
  • Difficulty Level : Easy
  • Last Updated : 31 Oct, 2017

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 Concept of setjump and longjump in C.




// CPP program to print one 100 times.
#include <iostream>
#include <setjmp.h>
using namespace std;
  
jmp_buf buf;
  
int main()
{
    int x = 1;
  
    // Setup jump position using buf
    setjmp(buf);
    cout << "1"; // Prints 1
  
    x++;
    if (x <= 100)
  
        // Jump to the point setup by setjmp
        longjmp(buf, 1);
  
    return 0;
}

Output :

 100 times 1.

The same can be written for C also.

This article is contributed by Aditya Rakhecha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :