给定一个数字N,我们需要打印从1到N的数字,而不需要直接递归、循环和标签。基本上,我们需要插入上面的代码片段,这样它就可以打印从1到N的数字了?
null
C
#include <stdio.h> #define N 20; int main() { // Your code goes Here. } |
例如:
Input : 10Output : 1 2 3 4 5 6 7 8 9 10Input : 5Output : 1 2 3 4 5
我们已经在以下帖子中讨论了解决方案: 在C++中打印1到100,没有循环和递归 如何在不使用循环的情况下打印从1到100的数字?
下面的代码可以打印从1到100的数字,而不需要直接递归、循环和标签。代码使用 间接递归 .
C++
// C++ program to print from 1 to N using // indirect recursion/ #include<bits/stdc++.h> 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<stdio.h> // 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 |
Python3
# 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 |
PHP
<?php // PHP program to print // from 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() function fun1() { global $N ; global $n ; if ( $n <= $N ) { echo $n , " " ; $n ++; fun2(); } else return ; } // Prints n, increments // n and calls fun2() function fun2() { global $N ; global $n ; if ( $n <= $N ) { echo $n , " " ; $n ++; fun1(); } else return ; } // Driver Code fun1(); // This code is contributed // by m_kit ?> |
Javascript
<script> // Javascript program to print from 1 to N using // indirect recursion // We can avoid use of these using references let N = 20; let n = 1 // Prints n, increments n and calls fun1() function fun1() { if (n <= N) { document.write( n+ " " ); n++; fun2(); } else { return ; } } // Prints n, increments n and calls fun2() function fun2() { if (n <= N) { document.write(n+ " " ); n++; fun1(); } else { return ; } } // Driver code fun1(); // This code is contributed by rag2127 </script> |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
这是怎么回事 在上面的程序中,我们只使用了两个函数。一个调用其他函数,另一个调用前一个函数,因此是间接递归。
练习: 修改上述程序,将N用作参数,而不是使其成为全局参数。
本文由 乌马赫斯瓦拉奥·图玛 属于 海得拉巴Jntuh工程学院 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END