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 loop, how else can be track something in C language!
It can be done in many ways to print numbers using any looping conditions such as for(), while(), do-while(). But the same can be done without using loops (using recursive functions, goto statement).
Printing numbers from 1 to 100 using recursive functions has already been discussed in Set-1 . In this post, other two methods have been discussed:
- Using goto statement:
C++
#include <iostream>usingnamespacestd;intmain(){inti = 0;begin:i = i + 1;cout << i <<" ";if(i < 100){gotobegin;}return0;}// This code is contributed by ShubhamCoderchevron_rightfilter_noneC
#include <stdio.h>intmain(){inti = 0;begin:i = i + 1;printf("%d ", i);if(i < 100)gotobegin;return0;}chevron_rightfilter_noneC#
usingSystem;classGFG{staticpublicvoidMain (){inti = 0;begin:i = i + 1;Console.Write(" "+ i +" ");if(i < 100){gotobegin;}}}// This code is contributed by ShubhamCoderchevron_rightfilter_noneOutput:1 2 3 4 . . . 97 98 99 100
- Using recursive main function:
C++
#include <iostream>usingnamespacestd;intmain(){staticinti = 1;if(i <= 100){cout << i++ <<" ";main();}return0;}// This code is contributed by ShubhamCoderchevron_rightfilter_noneC
#include <stdio.h>intmain(){staticinti = 1;if(i <= 100) {printf("%d ", i++);main();}return0;}chevron_rightfilter_noneJava
// Java program to count all pairs from both the// linked lists whose product is equal to// a given valueclassGFG{staticinti =1;publicstaticvoidmain(String[] args){if(i <=100){System.out.printf("%d ", i++);main(null);}}}// This code is contributed by Rajput-Jichevron_rightfilter_noneC#
// C# program to count all pairs from both the// linked lists whose product is equal to// a given valueusingSystem;classGFG{staticinti = 1;publicstaticvoidMain(String[] args){if(i <= 100){Console.Write("{0} ", i++);Main(null);}}}// This code is contributed by Rajput-Jichevron_rightfilter_noneOutput:1 2 3 4 . . . 97 98 99 100
Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

