RECUR13 Editorial

Problem Explanation

You are given an integer N and you have to output that number as a Binary String.

Approach

To convert a number from Decimal to Binary, we have to append the right most bit to the string and then right shift bits so we can access the next bit. We repeat this until there are no set bits in the number. We can extract the right-most bit using (N&1) or (N\%2). And we can right-shift N using the bitwise operator (N>>=1) or by dividing N by 2. To do this using recursion we assume that the function returns the Binary String of the number so we return the string after concatenating the Binary string returned by the function after passing the number right shifted by 1 and the right-most bit as a character. As for the base condition, when there are no set bits in the number, we return an empty string.

Code

def Binary(n):
    if(n==0):
        return ''
    return Binary(n//2) + str(n&1)

n = int(input())
print(Binary(n))