Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active December 7, 2020 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/b55270307b50dd981fd7720e7b687aed to your computer and use it in GitHub Desktop.
Save parzibyte/b55270307b50dd981fd7720e7b687aed to your computer and use it in GitHub Desktop.
"""
https://parzibyte.me/blog
"""
def decimal_a_binario(decimal):
if decimal <= 0:
return "0"
# Aquí almacenamos el resultado
binario = ""
# Mientras se pueda dividir...
while decimal > 0:
# Saber si es 1 o 0
residuo = int(decimal % 2)
# E ir dividiendo el decimal
decimal = int(decimal / 2)
# Ir agregando el número (1 o 0) a la izquierda del resultado
binario = str(residuo) + binario
return binario
decimal = int(input("Ingresa un número decimal: "))
binario = decimal_a_binario(decimal)
print(f"El número {decimal} es {binario} en binario")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment