Generate n | Function to convert decimal to binary ; leftmost digits are filled with 0 ; Function to generate gray code ; generate gray code of corresponding binary number of integer i . ; printing gray code ; Driver code
def decimalToBinaryNumber ( x , n ) : NEW_LINE INDENT binaryNumber = [ 0 ] * x ; NEW_LINE i = 0 ; NEW_LINE while ( x > 0 ) : NEW_LINE INDENT binaryNumber [ i ] = x % 2 ; NEW_LINE x = x // 2 ; NEW_LINE i += 1 ; NEW_LINE DEDENT for j in range ( 0 , n - i ) : NEW_LINE INDENT print ( '0' , end = " " ) ; NEW_LINE DEDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( binaryNumber [ j ] , end = " " ) ; NEW_LINE DEDENT DEDENT def generateGrayarr ( n ) : NEW_LINE INDENT N = 1 << n ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = i ^ ( i >> 1 ) ; NEW_LINE decimalToBinaryNumber ( x , n ) ; NEW_LINE print ( ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 ; NEW_LINE generateGrayarr ( n ) ; NEW_LINE DEDENT