Practice Questions for Recursion | Set 3 Last Updated : 20 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Explain the functionality of below recursive functions. Question 1 C++ void fun1(int n) { int i = 0; if (n > 1) fun1(n - 1); for (i = 0; i < n; i++) cout << " * "; } // This code is contributed by shubhamsingh10 C void fun1(int n) { int i = 0; if (n > 1) fun1(n-1); for (i = 0; i < n; i++) printf(" * "); } Java static void fun1(int n) { int i = 0; if (n > 1) fun1(n - 1); for (i = 0; i < n; i++) System.out.print(" * "); } // This code is contributed by shubhamsingh10 Python def fun1(n): i = 0 if (n > 1): fun1(n - 1) for i in range(n): print(" * ",end="") # This code is contributed by shubhamsingh10 C# static void fun1(int n) { int i = 0; if (n > 1) fun1(n-1); for (i = 0; i < n; i++) Console.Write(" * "); } // This code is contributed by shubhamsingh10 JavaScript <script> function fun1(n) { let i = 0; if (n > 1) fun1(n - 1); for(i = 0; i < n; i++) document.write(" * "); } // This code is contributed by gottumukkalabobby </script> Answer: Total numbers of stars printed is equal to 1 + 2 + .... (n-2) + (n-1) + n, which is n(n+1)/2. Time complexity: O(n2)Auxiliary Space: O(n), due to recursion call stackQuestion 2 C++ #define LIMIT 1000 void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return; cout << n <<" "; fun2(2*n); cout << n <<" "; } // This code is contributed by shubhamsingh10 C #define LIMIT 1000 void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return; printf("%d ", n); fun2(2*n); printf("%d ", n); } Java int LIMIT = 1000; void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return; System.out.print(String.format("%d ", n)); fun2(2 * n); System.out.print(String.format("%d ", n)); } Python LIMIT = 1000 def fun2(n): if (n <= 0): return if (n > LIMIT): return print(n, end=" ") fun2(2 * n) print(n, end=" ") # This code is contributed by shubhamsingh10 C# int LIMIT = 1000 void fun2(int n) { if (n <= 0) return; if (n > LIMIT) return; Console.Write(n+" "); fun2(2*n); Console.Write(n+" "); } // This code is contributed by Shubhamsingh10 JavaScript <script> let LIMIT = 1000; function fun2(n) { if (n <= 0) return; if (n > LIMIT) return; document.write(n + " ")); fun2(2 * n); document.write(n + " ")); } // This code is contributed by gottumukkalabobby </script> Answer: For a positive n, fun2(n) prints the values of n, 2n, 4n, 8n ... while the value is smaller than LIMIT. After printing values in increasing order, it prints same numbers again in reverse order. For example fun2(100) prints 100, 200, 400, 800, 800, 400, 200, 100. If n is negative, the function is returned immediately. Time complexity: O(log(limit/n)).Auxiliary Space: O(log(limit/n)), due to recursion call stackPlease write comments if you find any of the answers/codes incorrect, or you want to share more information about the topics discussed above. Comment More infoAdvertise with us Next Article Practice Questions for Recursion | Set 3 K kartik Follow Improve Article Tags : Recursion DSA Practice Tags : Recursion Similar Reads Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution 14 min read What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is 8 min read Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++ // C++ program to find factorial of given number #include<bits/stdc++.h> using namespace std; // ----- Recursion 6 min read Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord 15+ min read Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr 6 min read What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct 7 min read What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con 5 min read Why is Tail Recursion optimization faster than normal Recursion? What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recur 4 min read Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do 4 min read Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn 4 min read Like