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

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++



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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

    chevron_right

    
    

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    int main()
    {
        int i = 0;
    begin:
        i = i + 1;
        printf("%d ", i);
      
        if (i < 100)
            goto begin;
        return 0;
    }

    chevron_right

    
    

    Output:

    1 2 3 4 . . . 97 98 99 100
    
  2. Using recursive main function:

    C++

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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

    chevron_right

    
    

    C

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <stdio.h>
      
    int main()
    {
        static int i = 1;
        if (i <= 100) {
            printf("%d ", i++);
            main();
        }
        return 0;
    }

    chevron_right

    
    

    Java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to count all pairs from both the
    // linked lists whose product is equal to
    // a given value
    class 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

    chevron_right

    
    

    C#

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to count all pairs from both the
    // linked lists whose product is equal to
    // a given value
    using 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

    chevron_right

    
    

    Output:

    1 2 3 4 . . . 97 98 99 100
    

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up


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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.