Skip to content

Commit

Permalink
Reto mouredev#14 - Python
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoAlvarezSchiaffino committed Dec 19, 2023
1 parent 0c927fc commit 6800814
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
def convert_to_octal_hexadecimal(decimal_number):
"""
Convert a decimal number to Octal and Hexadecimal.
Args:
decimal_number (int): The decimal number to convert.
Returns:
tuple: A tuple containing the Octal and Hexadecimal representations.
"""
octal_result, hexadecimal_result = convert_to_base(decimal_number, 8), convert_to_base(decimal_number, 16)
return octal_result, hexadecimal_result

def convert_to_base(decimal, base):
"""
Convert a decimal number to the specified base.
Args:
decimal (int): The decimal number to convert.
base (int): The base to convert to.
Returns:
str: The converted number in the specified base.
"""
digits = "0123456789ABCDEF"
result = []

while decimal >= base:
result.append(digits[decimal % base])
decimal //= base

result.append(digits[decimal])
reversed_result = result[::-1]

return "".join(reversed_result)

decimal_number = 255
octal, hexadecimal = convert_to_octal_hexadecimal(decimal_number)
print(f"The decimal number {decimal_number} converts to octal as {octal}")
print(f"The decimal number {decimal_number} converts to hexadecimal as {hexadecimal}")

0 comments on commit 6800814

Please sign in to comment.