Represent a number N in base | Function to convert N to equivalent representation in base - 2 ; Stores the required answer ; Iterate until N is not equal to zero ; If N is Even ; Add char '0' in front of string ; Add char '1' in front of string ; Decrement N by 1 ; Divide N by - 2 ; If string is empty , that means N is zero ; Put '0' in string s ; Given Input ; Function Call
def BaseConversion ( N ) : NEW_LINE INDENT s = " " NEW_LINE while ( N != 0 ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT s = "0" + s NEW_LINE DEDENT else : NEW_LINE INDENT s = "1" + s NEW_LINE N -= 1 NEW_LINE DEDENT N /= - 2 NEW_LINE DEDENT if ( s == " " ) : NEW_LINE INDENT s = "0" NEW_LINE DEDENT return s NEW_LINE DEDENT N = - 9 NEW_LINE print ( BaseConversion ( N ) ) NEW_LINE