Skip to content

Commit

Permalink
program to convert decimal to binary using recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
riyaa060 committed Oct 15, 2024
1 parent 80d3d7d commit 0916336
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Python/decimal to binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# Convert a decimal number to binary using concept of "Recursion":
def decimal_to_binary(num):
if num == 0:
return ""
else:
return decimal_to_binary(num // 2) + str(num % 2)


# Example usage
decimal_number = 42
binary_number = decimal_to_binary(decimal_number)
if binary_number == "":
binary_number = "0" # Handle case for zero
print(f"The binary representation of {decimal_number} is {binary_number}.")

0 comments on commit 0916336

Please sign in to comment.