# Practice Questions for Recursion | Set 6

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'``) ` `        ``return``; ` `  `  `    ``abc(s + 1); ` `    ``abc(s + 1); ` `    ``cout << s;     ` `} ` ` `  `// This code is contributed by shubhamsingh10 `

## C

 `void` `abc(``char` `*s) ` `{ ` `    ``if``(s == ``'\0'``) ` `        ``return``; ` ` `  `    ``abc(s + 1); ` `    ``abc(s + 1); ` `    ``printf``(``"%c"``, s);     ` `} `

## Java

 `static` `void` `abc(``char` `*s) ` `{ ` `    ``if``(s[``0``] == ``'\0'``) ` `        ``return``; ` `   `  `    ``abc(s + ``1``); ` `    ``abc(s + ``1``); ` `    ``System.out.print(s[``0``]);     ` `} ` ` `  `// This code is contributed by shubhamsingh10 `

## Python3

 `def` `abc(s): ` `    ``if``(``len``(s) ``=``=` `0``): ` `        ``return` `     `  `    ``abc(s[``1``:]) ` `    ``abc(s[``1``:]) ` `    ``print``(s[``0``]) ` `     `  `# This code is contributed by shubhamsingh10 `

## C#

 `static` `void` `abc(``char` `*s) ` `{ ` `    ``if``(s == ``'\0'``) ` `        ``return``; ` ` `  `    ``abc(s + 1); ` `    ``abc(s + 1); ` `    ``Console.Write(s);  ` `} ` ` `  `// This code is contributed by shubhamsingh10 `

Following is the relation between num and len.

` num = 2^len-1 `
```s is 1 time printed
s is 2 times printed
s is 4 times printed
s[i] is printed 2^i times
s[strlen(s)-1] is printed 2^(strlen(s)-1) times
total = 1+2+....+2^(strlen(s)-1)
= (2^strlen(s)) - 1
```

For example, the following program prints 7 characters.

## C++

 `#include ` `using` `namespace` `std; ` ` `  `void` `abc(``char` `s[]) ` `{ ` `    ``if``(s == ``'\0'``) ` `        ``return``; ` ` `  `    ``abc(s + 1); ` `    ``abc(s + 1); ` `    ``cout << s; ` `} ` ` `  `int` `main() ` `{ ` `    ``abc(``"xyz"``); ` `    ``return` `0; ` `} ` `//This code is contributed by shubhamsingh10 `

## C

 `#include ` ` `  `void` `abc(``char` `*s) ` `{ ` `    ``if``(s == ``'\0'``) ` `        ``return``; ` ` `  `    ``abc(s + 1); ` `    ``abc(s + 1); ` `    ``printf``(``"%c"``, s); ` `} ` ` `  `int` `main() ` `{ ` `    ``abc(``"xyz"``); ` `    ``return` `0; ` `} `

## Python3

 `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 `

Thanks to bharat nag for suggesting this solution.

Question 2

## C++

 `#include ` `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 ` `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 `

## Python3

 `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 `

Output:

``` 1
2
3
3
3
3
3
```

The 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 count is not incremented and no more functions are called as the if 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 to fun(fun(3)). This continues and we get “3” printed 5 times on the screen.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : SHUBHAMSINGH10

Article Tags :
Practice Tags :

9

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.