1a. Write a recursive algorithm that converts a decimal number D in base 10 (between 0 and 1023) to a binary number B in base 2. A base 10 number uses the ten digits 0, 1, …, 9, while a base 2 number uses only the two digits 0 and 1. For example: D: 29 B: 11101 (since 1*24 + 1*23 + 1*2 2 + 0*21 + 1*20 = 29) D: 999 B: 1111100111 1b.) Trace your algorithm for D = 13
Here, this works for any base up to 36 #!/usr/bin/python characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" def mkDigit( number, base ): if( number == 0 ): return "" index = number % base number = number // base return mkDigit( number, base ) + characters[ index ] if( __name__ == "__main__" ): number = int( raw_input( "Enter the number: " ) ) base = 0 while( base < 2 or base >= len( characters ) ): base = int( raw_input( "Enter the base ( 2 - " + str( len( characters ) -1 ) + " ): " ) ) print mkDigit( number, base )
thanks, can this be implemented in Java? or is it C?
It's python. But it could be implemented in any programming language.
thanks!
Can some one also help me with the 2nd Question as well? i have done 3 and 4 this is the last one i have to do. Its large so its attached as a Word document. if you could send it back as an attachment that would be great.
What have you tried so far? Where are you stuck?
Join our real-time social learning platform and learn together with your friends!