...

/

Solution Review: Convert Decimal Number to Binary Number

Solution Review: Convert Decimal Number to Binary Number

This review provides a detailed analysis of the solution to convert a decimal number to binary number.

We'll cover the following...

Solution: Using Recursion

Press + to interact
def decimalToBinary(testVariable) :
# Base Case
if testVariable <= 1:
return str(testVariable)
# Recursive Case
else:
return decimalToBinary(testVariable // 2) + decimalToBinary(testVariable % 2) # Floor division -
# division that results into whole number adjusted to the left in the number line
# Driver Code
testVariable = 11
print(decimalToBinary(testVariable))

Explanation:

To convert a decimal number to a binary number, keep track of the remainderremainder and the remaining dividenddividend ...