📌  相关文章
📜  在不使用循环的情况下,如何打印从1到100的数字? |套装2

📅  最后修改于: 2021-05-25 21:25:24             🧑  作者: Mango

如果我们仔细地看一下这个问题,我们可以看到“循环”的想法是跟踪一些计数器值,例如“ i = 0”直到“ i <= 100”。因此,如果我们不允许使用循环,还有其他方法可以用C语言跟踪!可以使用任何循环条件(例如for(),while(),do-while())以多种方式打印数字。但是,无需使用循环(使用递归函数,goto语句)也可以做到这一点。

在Set-1中已经讨论了使用递归函数打印从1到100的数字。在这篇文章中,讨论了其他两种方法:

  1. 使用goto语句:
    C++
    #include 
    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 
      
    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


    C++
    #include 
    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 
      
    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


    输出:
    1 2 3 4 . . . 97 98 99 100
    
  2. 使用递归主要函数:

    C++

    #include 
    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 
      
    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
    
    输出:
    1 2 3 4 . . . 97 98 99 100
    
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。