Practice Questions for Recursion | Set 2 Last Updated : 25 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Explain the functionality of the following functions. Question 1 C++ /* Assume that n is greater than or equal to 1 */ int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } Java /* Assume that n is greater than or equal to 1 */ static int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } Python3 # Assume that n is greater than or equal to 1 */ def fun1(n): if(n == 1): return 0 else: return 1 + fun1(n//2) C# /* Assume that n is greater than or equal to 1 */ static int fun1(int n) { if (n == 1) return 0; else return 1 + fun1(n / 2); } JavaScript <script> /* Assume that n is greater than or equal to 1 */ function fun1(n) { if (n == 1) return 0 else return 1 + fun1(Math.floor(n / 2)) } </script> Answer: The function calculates and returns For example, if n is between 8 and 15 then fun1() returns 3. If n is between 16 to 31 then fun1() returns 4. Question 2 C++ /* Assume that n is greater than or equal to 0 */ void fun2(int n) { if(n == 0) return; fun2(n/2); cout << n%2 << endl; } C /* Assume that n is greater than or equal to 0 */ void fun2(int n) { if(n == 0) return; fun2(n/2); printf("%d", n%2); } Java /* Assume that n is greater than or equal to 1 */ static void fun2(int n) { if(n == 0) return; fun2(n/2); System.out.println(n%2); } Python3 # Assume that n is greater than or equal to 0 */ def fun2(n): if(n == 0): return fun2(n // 2) print(n % 2, end="") C# void fun2(int n) { if(n == 0) return; fun2(n/2); Console.Write(n%2); } JavaScript <script> // Assume that n is greater than or equal to 1 function fun2(n) { if (n == 0) return; fun2(Math.floor(n / 2)); document.write(n % 2 + " ") } </script> Auxiliary Space: O(log2N), due to recursion call stack Time Complexity: O(log N) Answer: The function fun2() prints the binary equivalent of n. For example, if n is 21 then fun2() prints 10101. Note: Above functions are just for practicing recursion, they are not the ideal implementation of the functionality they provide. Please write comments if you find any of the answers/codes incorrect. Comment More infoAdvertise with us Next Article Practice Questions for Recursion | Set 2 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