Practice Recursion Hard (original) (raw)

Last Updated : 10 Apr, 2026

**Question 1: Predict the output of the following program. What does the following fun() do in general?

C++ `

using namespace std; int fun(int a, int b) { if (b == 0) return 0; if (b % 2 == 0) return fun(a + a, b/2);

return fun(a + a, b/2) + a; 

}

// Driver code int main() { cout << fun(4, 3) ; return 0; }

C

int fun(int a, int b) { if (b == 0) return 0; if (b % 2 == 0) return fun(a+a, b/2);

return fun(a+a, b/2) + a; }

// Driver code int main() { printf("%d", fun(4, 3)); getchar(); return 0; }

Java

class GFG { static int fun(int a, int b) { if (b == 0) return 0; if (b % 2 == 0) return fun(a + a, b/2);

    return fun(a + a, b/2) + a; 
}

// Driver code
public static void main (String[] args) 
{
    System.out.println(fun(4, 3));
}

}

Python

def fun(a, b): if (b == 0): return 0 if (b % 2 == 0): return fun(a + a, b//2)

return fun(a + a, b//2) + a 

Driver code

print(fun(4, 3))

C#

using System;

class GFG{

static int fun(int a, int b) 
{ 
    if (b == 0) 
        return 0; 
    if (b % 2 == 0) 
        return fun(a + a, b/2); 
    
    return fun(a + a, b/2) + a; 
} 

// Driver code
static public void Main () 
{ 
    Console.Write(fun(4, 3)); 
} 

}

JavaScript

function fun(a, b) { if (b == 0) return 0; if (b % 2 == 0) return fun(a + a, Math.floor(b/2));

return fun(a + a, Math.floor(b/2)) + a;

}

// Driver code console.log(fun(4, 3));

`

It calculates a*b (a multiplied b).

**Question 2 : In question 1, if we replace + with * and replace return 0 with return 1, then what does the changed function do? Following is the changed function.

C++ `

#include using namespace std;

int fun(int a, int b) { if (b == 0) return 1; if (b % 2 == 0) return fun(a*a, b/2);

return fun(a*a, b/2)*a; }

int main() { cout << fun(4, 3) ; return 0; }

C

#include<stdio.h>

int fun(int a, int b) { if (b == 0) return 1; if (b % 2 == 0) return fun(a*a, b/2);

return fun(a*a, b/2)*a; }

int main() { printf("%d", fun(4, 3)); return 0; }

Java

import java.io.*;

class GFG { static int fun(int a, int b) { if (b == 0) return 1; if (b % 2 == 0) return fun(a*a, b/2);

    return fun(a*a, b/2)*a; 
} 

public static void main (String[] args)
{ 
    System.out.println(fun(4, 3)); 
} 

}

Python

def fun(a, b): if (b == 0): return 1 if (b % 2 == 0): return fun(a*a, b//2)

return fun(a*a, b//2)*a 

Driver code

print(fun(4, 3))

C#

using System;

public class GFG{

static int fun(int a, int b) 
{ 
    if (b == 0) 
        return 1; 
    if (b % 2 == 0) 
        return fun(a*a, b/2); 
    
    return fun(a*a, b/2)*a; 
} 


static public void Main ()
{ 
    Console.WriteLine(fun(4, 3)); 
} 

}

JavaScript

function fun(a, b) { if (b == 0) { return 1; } if (b % 2 == 0) { return fun(a * a, Math.floor(b / 2)); } return fun(a * a, Math.floor(b / 2)) * a; }

// Driver code console.log(fun(4, 3));

`

It calculates a^b (a raised to power b).

**Question 3 : Predict the output of the following program. What does the following fun() do in general?

C++ `

#include using namespace std;

int fun(int n) { if (n > 100) return n - 10; return fun(fun(n+11)); }

int main() { cout << " " << fun(99) << " "; getchar(); return 0; }

// This code is contributed by Shubhamsingh10

C

#include<stdio.h>

int fun(int n) { if (n > 100) return n - 10; return fun(fun(n+11)); }

int main() { printf(" %d ", fun(99)); getchar(); return 0; }

Java

import java.io.*;

class GFG { static int fun(int n) { if (n > 100) return n - 10; return fun(fun(n+11)); }

public static void main (String[] args) {
    System.out.println(" " + fun(99) + " ");
}

} // This code is contributed by Shubhamsingh10

Python

def fun(n):

if (n > 100):
    return n - 10

return fun(fun(n + 11))

Driver code

print(fun(99))

This code is contributed by Shubhamsingh10

C#

using System;

class GFG{

static int fun(int n) { if (n > 100) return n - 10; return fun(fun(n + 11)); }

// Driver code
static public void Main () { Console.WriteLine(fun(99)); } }

// This code is contributed by Shubhamsingh10

JavaScript

`

fun(99) = fun(fun(110)) since 99 ? 100
= fun(100) since 110 > 100
= fun(fun(111)) since 100 ? 100
= fun(101) since 111 > 100
= 91 since 101 > 100

Answer: The returned value of fun() is 91 for all integer arguments n 101. This function is known as McCarthy 91 function.

**Question 4 : Consider the following recursive 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++ `

using namespace std; void abc(char *s) { if(s[0] == '\0') return;

abc(s + 1);
abc(s + 1);
cout << s[0];    

}

// Driver code int main() {

abc("xyz");

}

C

void abc(char *s) { if(s[0] == '\0') return;

abc(s + 1);
abc(s + 1);
printf("%c", s[0]);    

}

// Driver code int main() {

abc("xyz");
return 0;

}

Java

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));
}

// Driver code
public static void main(String[] args) { abc("xyz"); }

}

Python

def abc(s): if(len(s) == 0): return

abc(s[1:])
abc(s[1:])
print(s[0],end="")

Driver code

abc("xyz")

C#

using System;

public 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 public void Main() { abc("xyz"); }

}

JavaScript

function abc(s) { if (s.length == 0) return;

abc(s.substring(1));
abc(s.substring(1));
console.log(s[0]);

}

// Driver code abc("xyz")

`

Answer: The following relathionsip between **num and **len: num = 2^len - 1

s[0] is 1 time printed
s[1] is 2 times printed
s[2] 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

**Question 5 : Guess the output of the following code.

C++ `

using namespace std;

int fun(int count) { cout << count << endl; if(count < 3) { fun(fun(fun(++count))); } return count; }

// Driver code int main() { fun(1); return 0; }

C

int fun(int count) { printf("%d\n", count); if(count < 3) { fun(fun(fun(++count))); } return count; }

// Driver code int main() { fun(1); return 0; }

Java

class GFG { static int fun(int count) { System.out.println(count); if (count < 3) { fun(fun(fun(++count))); } return count; }

// Driver code
public static void main(String[] args) { fun(1); }

}

Python

def fun(count): print(count) if(count < 3): count+=1 fun(fun(fun(count)))

return count 

Driver code

fun(1)

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);   
} 

}

JavaScript

function fun(count) { console.log(count); if(count < 3) { fun(fun(fun(++count))); } return count; }

fun(1);

`

Answer: The main() function calls fun(1). fun(1) prints 1 and calls fun(fun(fun(2))). Then fun(2) prints 2 and calls fun(fun(fun(3))). At fun(3), the condition count < 3 becomes false, so it only prints 3 and returns 3. Now all the nested calls start resolving. Each remaining call becomes fun(3), which again prints 3 and returns 3. This process repeats as the nested calls unwind one by one. As a result, 3 gets printed multiple times (5 times in total).

**Question 6 : Predict the output of the following program.

C++ `

using namespace std; void fun(int n) { if (n > 0) { fun(n - 1); cout << n << " "; fun(n - 1); } }

int main() { fun(4); return 0; }

C

void fun(int n) { if (n > 0) { fun(n - 1); printf("%d ", n); fun(n - 1); } }

int main() { fun(4); return 0; }

Java

class GFG { static void fun(int n) { if (n > 0) { fun(n - 1); System.out.print(n + " "); fun(n - 1); } }

// Driver code
public static void main(String[] args) { fun(4); }

}

Python

def fun(n):

if(n > 0):
    fun(n - 1)
    print(n, end=" ")
    fun(n - 1)

driver code

fun(4)

C#

using System;

class GFG { static void fun(int n) { if (n > 0) { fun(n - 1); Console.Write(n + " "); fun(n - 1); } }

// Driver code
static public void Main() { fun(4); }

}

JavaScript

function fun(n) { if (n > 0) fun(n - 1); console.log(n + " ") fun(n - 1); }

// driver code fun(4)

`

               fun(4)  
               /  
            fun(3), print(4), fun(3) [fun(3) prints 1 2 1 3 1 2 1]  
           /  
       fun(2), print(3), fun(2) [fun(2) prints 1 2 1]  
       /  
   fun(1), print(2), fun(1) [fun(1) prints 1]  
   /  
fun(0), print(1), fun(0) [fun(0) does nothing]

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.