Python program that converts a given decimal number to binary, octal and hexadecimal:

Created with Sketch.

Python program that converts a given decimal number to binary, octal and hexadecimal:

decimal = int(input("Enter a decimal number: "))

# Convert decimal to binary
binary = bin(decimal)
print("Binary representation of", decimal, "is", binary[2:])

# Convert decimal to octal
octal = oct(decimal)
print("Octal representation of", decimal, "is", octal[2:])

# Convert decimal to hexadecimal
hexadecimal = hex(decimal)
print("Hexadecimal representation of", decimal, "is", hexadecimal[2:])

The program prompts the user to enter a decimal number using the input() function, and stores the value in the variable “decimal”. Then it uses the built-in functions bin(), oct() and hex() to convert the decimal number to binary, octal and hexadecimal respectively. The built-in functions return the number in the form of a string, in which the first two characters are ‘0b’ for binary, ‘0o’ for octal and ‘0x’ for hexadecimal representation, so we are slicing the string from the third character to get the actual binary, octal and hexadecimal representation.

Leave a Reply

Your email address will not be published. Required fields are marked *