Practice Questions for Recursion | Set 2 - GeeksforGeeks

12 May, 2022
Explain the functionality of following functions. 
Question 1 
C++
Java
Python3
C#
Javascript
/* 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);} // This code is contributed by shubhamsingh10// Improved by Adwitiya Mourya
/* 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);} // This code is contributed by shubhamsingh10
# Assume that n is greater than or equal to 1 */def fun1(n):    if(n == 1):        return 0    else:        return 1 + fun1(n//2) # This code is contributed by shubhamsingh10
/* 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);} // This code is contributed by shubhamsingh10
<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))} // This code is contributed by gottumukkalabobby </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++
C
Java
Python3
C#
Javascript
/* 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;} //This code is contributed by shubhamsingh10
/* 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);} 
/* 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);} // This code is contributed by Shubhamsingh10
# Assume that n is greater than or equal to 0 */def fun2(n):    if(n == 0):        return         fun2(n / 2)    print(n % 2, end="") # This code is contributed by shubhamsingh10
void fun2(int n){if(n == 0)    return;   fun2(n/2);Console.Write(n%2);}// This code is contributed by shubhamsingh10
<script> // Assume that n is greater than or equal to 1function fun2(n){    if (n == 0)        return;         fun2(Math.floor(n / 2));    document.write(n % 2 + " ")} // This code is contributed by gottumukkalabobby </script>
Answer: The function fun2() prints binary equivalent of n. For example, if n is 21 then fun2() prints 10101. Note that 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. 
SHUBHAMSINGH10
gottumukkalabobby
smlakhera
adwitiya5727
beingharsh
borysenkomaksym
Misc
Recursion
Misc
Recursion
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to write Regular Expressions?
Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)
Association Rule
Recursive Functions
Activation Functions
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Program for Tower of Hanoi
Backtracking | Introduction
Print all possible combinations of r elements in a given array of size n