Questions
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Last Updated : 18 Jul 2025
Jul 24, Jan 24
Solution:
# Get a number from the user
num = int(input("Enter a number: "))
# Print the binary representation
print("Binary:", bin(num))
# Print the octal representation
print("Octal:", oct(num))
# Print the hexadecimal representation
print("Hexadecimal:", hex(num))
OUTPUT:
Enter a number: 13
Binary: 0b1101
Octal: 0o15
Hexadecimal: 0xd
Explanation:
- Input: Read an integer from the user.
- Conversions:
bin(num)
: Binary format (prefix0b
).oct(num)
: Octal format (prefix0o
).hex(num)
: Hexadecimal format (prefix0x
).
- Output: Prints the number in binary, octal, and hexadecimal formats.
Meanwhile you can watch this video
Watch Video