原文:

给定一个数字 n,我们需要打印从 1 到 n 的数字,不需要直接递归、循环和标签。基本上我们需要插入上面的代码片段,这样它就可以打印从 1 到 n 的数字?

c

#include 
#define n 20;
int main()
{
   // your code goes here.
}

示例:

input  : 10
output : 1 2 3 4 5 6 7 8 9 10
input  : 5
output : 1 2 3 4 5

我们已经在下面的帖子中讨论了pg电子试玩链接的解决方案:

下面的代码可以打印从 1 到 100 的数字,不需要直接递归、循环和标签。代码使用。

c

// c   program to print from 1 to n using
// indirect recursion/
#include
using namespace std;
// we can avoid use of these using references
int n = 20;
int n = 1;
void fun1();
void fun2();
// prints n, increments n and calls fun1()
void fun1()
{
    if (n <= n)
    {
        cout << n << " ";
        n  ;
        fun2();
    }
    else
        return;
}
// prints n, increments n and calls fun2()
void fun2()
{
    if (n <= n)
    {
        cout << n << " ";
        n  ;
        fun1();
    }
    else
        return;
}
// driver program
int main()
{
    fun1();
    return 0;
}
// this code is contributed by pankajsharmagfg.

c

// c program to print from 1 to n using
// indirect recursion/
#include
// we can avoid use of these using references
#define n 20;
int n = 1;
// prints n, increments n and calls fun1()
void fun1()
{
    if (n <= n)
    {
        printf("%d", n);
        n  ;
        fun2();
    }
    else
        return;
}
// prints n, increments n and calls fun2()
void fun2()
{
    if (n <= n)
    {
        printf("%d", n);
        n  ;
        fun1();
    }
    else
        return;
}
// driver program
int main(void)
{
    fun1();
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print from 1 to n using
// indirect recursion
class gfg
{
    // we can avoid use of these using references
    static final int n = 20;
    static int n = 1;
    // prints n, increments n and calls fun1()
    static void fun1()
    {
        if (n <= n)
        {
            system.out.printf("%d ", n);
            n  ;
            fun2();
        }
        else
        {
            return;
        }
    }
    // prints n, increments n and calls fun2()
    static void fun2()
    {
        if (n <= n)
        {
            system.out.printf("%d ", n);
            n  ;
            fun1();
        }
        else
        {
            return;
        }
    }
    // driver program
    public static void main(string[] args)
    {
        fun1();
    }
}
// this code is contributed by rajput-ji

python 3

# python program to prfrom 1 to n using
# indirect recursion
# we can avoid use of these using references
n = 20;
n = 1;
# prints n, increments n and calls fun1()
def fun1():
    global n, n;
    if (n <= n):
        print(n, end = " ");
        n  = 1;
        fun2();
    else:
        return;
# prints n, increments n and calls fun2()
def fun2():
    global n, n;
    if (n <= n):
        print(n, end = " ");
        n  = 1;
        fun1();
    else:
        return;
# driver program
if __name__ == '__main__':
    fun1();
# this code is contributed by 29ajaykumar

c

// c# program to print from 1 to n using
// indirect recursion
using system;
class gfg
{
    // we can avoid use of these using references
    static readonly int n = 20;
    static int n = 1;
    // prints n, increments n and calls fun1()
    static void fun1()
    {
        if (n <= n)
        {
            console.write("{0} ", n);
            n  ;
            fun2();
        }
        else
        {
            return;
        }
    }
    // prints n, increments n and calls fun2()
    static void fun2()
    {
        if (n <= n)
        {
            console.write("{0} ", n);
            n  ;
            fun1();
        }
        else
        {
            return;
        }
    }
    // driver code
    public static void main(string[] args)
    {
        fun1();
    }
}
// this code is contributed by rajput-ji

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

这是如何工作的?: 在上面的程序中,我们只使用了两个函数。一个叫别人,另一个叫前一个,所以间接递归。

练习: 修改上面的程序,使用 n 作为参数,而不是使其全局化。

本文由海得拉巴 jntuh 工程学院的供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。