Как вы будете печатать числа от 1 до 100 без использования цикла? | Комплект-2

Опубликовано: 16 Января, 2022

Если мы внимательно рассмотрим эту проблему, мы увидим, что идея «цикла» заключается в отслеживании некоторого значения счетчика, например, от «i = 0» до «i <= 100». Итак, если нам не разрешено использовать цикл -А как еще можно отследить что-то на языке Си!

Это можно сделать разными способами, чтобы напечатать числа с использованием любых условий цикла, таких как for (), while (), do-while (). Но то же самое можно сделать и без использования циклов (с использованием рекурсивных функций, оператора goto).

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

    #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

    C

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

    C#

    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
    Output:
    1 2 3 4 . . . 97 98 99 100
    
  2. Using recursive main function:

    C++

    #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

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

    Java

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

    C#

    // 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
    Output:
    1 2 3 4 . . . 97 98 99 100
    
Want to learn from the best curated videos and practice problems, check out the C Foundation Course for Basic to Advanced C.



C