How will you print numbers from 1 to 100 without using loop? | Set-2 - GeeksforGeeks

30 Jun, 2021
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: 
1. Using goto statement: 
C++
C
C#
#include <iostream>using namespace std; int main(){    int i = 0;     begin:    i = i + 1;    cout << i << " ";     if (i < 100)    {        goto begin;    }    return 0;} // This code is contributed by ShubhamCoder
#include <stdio.h> int main(){    int i = 0;begin:    i = i + 1;    printf("%d ", i);     if (i < 100)        goto begin;    return 0;}
using System; class GFG{ static public void Main (){    int i = 0;    begin:        i = i + 1;        Console.Write(" " + i + " ");         if (i < 100)        {            goto begin;        }}} // This code is contributed by ShubhamCoder
1 2 3 4 . . . 97 98 99 100
 
2. Using recursive main function: 
C++
C
Java
Python3
C#
// C++ program to count all pairs from both the// linked lists whose product is equal to// a given value#include <iostream>using namespace std; int main(){    static int i = 1;         if (i <= 100)    {        cout << i++ << " ";        main();    }    return 0;} // This code is contributed by ShubhamCoder
// C program to count all pairs from both the// linked lists whose product is equal to// a given value#include <stdio.h> int main(){    static int i = 1;    if (i <= 100) {        printf("%d ", i++);        main();    }    return 0;}
// Java program to count all pairs from both the// linked lists whose product is equal to// a given valueclass GFG{    static int i = 1;     public static void main(String[] args)    {         if (i <= 100)        {            System.out.printf("%d ", i++);            main(null);        }    }} // This code is contributed by Rajput-Ji
# Python3 program to count all pairs from both# the linked lists whose product is equal to# a given valuedef main(i):         if (i <= 100):        print(i, end = " ")        i = i + 1        main(i)         i = 1main(i) # This code is contributed by SoumikMondal
// C# program to count all pairs from both the// linked lists whose product is equal to// a given valueusing System; class GFG{    static int i = 1;     public static void Main(String[] args)    {        if (i <= 100)        {            Console.Write("{0} ", i++);            Main(null);        }    }} // This code is contributed by Rajput-Ji
1 2 3 4 . . . 97 98 99 100
 
 
Rajput-Ji
shubhamsingh84100
SoumikMondal
cpp-puzzle
C Language
Recursion
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multidimensional Arrays in C / C++
Left Shift and Right Shift Operators in C/C++
fork() in C
Function Pointer in C
Substring in C++
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction