Explain the functionality of 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); ` `} ` ` ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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` `); ` `} ` ` ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## 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` `) ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## 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); ` `} ` ` ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

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; ` `} ` ` ` `//This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

## 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` `); ` `} ` ` ` `// This code is contributed by Shubhamsingh10 ` |

*chevron_right*

*filter_none*

## 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` `=` `"") ` ` ` `# This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C#

`void` `fun2(` `int` `n) ` `{ ` `if` `(n == 0) ` ` ` `return` `; ` ` ` `fun2(n/2); ` `Console.Write(n%2); ` `} ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Practice Questions for Recursion | Set 3
- Practice Questions for Recursion | Set 4
- Practice Questions for Recursion | Set 5
- Practice Questions for Recursion | Set 6
- Practice Questions for Recursion | Set 7
- Practice Questions for Recursion | Set 1
- Practice questions for Linked List and Recursion
- Recursive Practice Problems with Solutions
- Product of 2 numbers using recursion | Set 2
- Count Set-bits of number using Recursion
- Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
- Algorithms Sample Questions | Recurrences | Set 2
- Algorithms Sample Questions | Set 3 | Time Order Analysis
- Reverse a stack using recursion
- Print 1 to 100 in C++, without loop and recursion
- Tail Recursion
- Recursion
- Mutual Recursion with example of Hofstadter Female and Male sequences
- Remove duplicates from a sorted linked list using recursion
- Reverse a Doubly linked list using recursion