Practice Questions for Recursion | Set 6 Last Updated : 22 Jul, 2024 Comments Improve Suggest changes Like Article Like Report Question 1 Consider the following recursive C function. Let len be the length of the string s and num be the number of characters printed on the screen. Give the relation between num and len where len is always greater than 0. C++ void abc(char *s) { if(s[0] == '\0') return; abc(s + 1); abc(s + 1); cout << s[0]; } C void abc(char *s) { if(s[0] == '\0') return; abc(s + 1); abc(s + 1); printf("%c", s[0]); } Java void abc(String s) { if (s.length() == 0) return; abc(s.substring(1)); abc(s.substring(1)); System.out.print(s.charAt(0)); } Python def abc(s): if(len(s) == 0): return abc(s[1:]) abc(s[1:]) print(s[0],end="") C# void abc(string s) { if (s.Length == 0) return; abc(s.Substring(1)); abc(s.Substring(1)); Console.Write(s[0]); } JavaScript function abc(s) { if (s.length == 0) return; abc(s.substring(1)); abc(s.substring(1)); console.log(s[0]); } The following is the relationship between num and len. num = 2^len-1s[0] is 1 time printeds[1] is 2 times printeds[2] is 4 times printeds[i] is printed 2^i timess[strlen(s)-1] is printed 2^(strlen(s)-1) timestotal = 1+2+....+2^(strlen(s)-1) = (2^strlen(s)) - 1For example, the following program prints 7 characters. C++ #include <bits/stdc++.h> using namespace std; void abc(char s[]) { if(s[0] == '\0') return; abc(s + 1); abc(s + 1); cout << s[0]; } int main() { abc("xyz"); return 0; } //This code is contributed by shubhamsingh10 C #include<stdio.h> void abc(char *s) { if(s[0] == '\0') return; abc(s + 1); abc(s + 1); printf("%c", s[0]); } int main() { abc("xyz"); return 0; } Java public class GFG { static void abc(String s) { if(s.length() == 0) return; abc(s.substring(1)); abc(s.substring(1)); System.out.print(s.charAt(0)); } public static void main(String[] args) { abc("xyz"); } } // This code is contributed by divyeh072019 Python def abc(s): if(len(s) == 0): return abc(s[1:]) abc(s[1:]) print(s[0],end="") # Driver code abc("xyz") # This code is contributed by shubhamsingh10 C# using System; class GFG { static void abc(string s) { if(s.Length == 0) return; abc(s.Substring(1)); abc(s.Substring(1)); Console.Write(s[0]); } // Driver code static void Main() { abc("xyz"); } } // This code is contributed by divyeshrabadiya07 JavaScript function abc(s) { if (s.length == 0) return; abc(s.substring(1)); abc(s.substring(1)); console.log(s[0]); } abc("xyz"); Question 2 : Guess the output of the following code. C++ #include <iostream> using namespace std; int fun(int count) { cout << count << endl; if(count < 3) { fun(fun(fun(++count))); } return count; } int main() { fun(1); return 0; } // This code is contributed by Shubhamsingh10 C #include<stdio.h> int fun(int count) { printf("%d\n", count); if(count < 3) { fun(fun(fun(++count))); } return count; } int main() { fun(1); return 0; } Java import java.util.*; class GFG{ static int fun(int count) { System.out.println(count); if(count < 3) { fun(fun(fun(++count))); } return count; } public static void main(String[] args) { fun(1); } } // This code is contributed by Shubhamsingh10 Python def fun(count): print(count) if(count < 3): count+=1 fun(fun(fun(count))) return count if __name__=="__main__": fun(1) # This code is contributed by Shubhamsingh10 C# using System; class GFG{ static int fun(int count) { Console.Write(count+"\n"); if(count < 3) { fun(fun(fun(++count))); } return count; } static public void Main () { fun(1); } } // This code is contributed by shubhamsingh10 JavaScript <script> function fun(count) { document.write(count + "</br>"); if(count < 3) { fun(fun(fun(++count))); } return count; } fun(1); </script> Output: 1 2 3 3 3 3 3The main() function calls fun(1). fun(1) prints "1" and calls fun(fun(fun(2))). fun(2) prints "2" and calls fun(fun(fun(3))). So the function call sequence becomes fun(fun(fun(fun(fun(3))))). fun(3) prints "3" and returns 3 (note that the count is not incremented and no more functions are called as if the condition is not true for count 3). So the function call sequence reduces to fun(fun(fun(fun(3)))). fun(3) again prints "3" and returns 3. So the function call again reduces to fun(fun(fun(3))) which again prints "3" and reduces it to fun(fun(3)). This continues and we get "3" printed 5 times on the screen. Please write comments if you find any of the answers/codes incorrect, or you want to share more information/questions about the topics discussed above. Comment More infoAdvertise with us Next Article Practice Questions for Recursion | Set 6 K kartik Follow Improve Article Tags : Misc Recursion DSA Practice Tags : MiscRecursion 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