Python Program to Convert Decimal to Binary, Octal, and Hexadecimal – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Convert Decimal to Binary, Octal, and Hexadecimal.

Code

# Python Program to Convert Decimal to Binary, Octal, and Hexadecimal
dec = int(input('Enter a positive number -> '))

print(f"{dec} in binary is {bin(dec)}")
print(f"{dec} in octal is {oct(dec)}")
print(f"{dec} in hexadecimal is {hex(dec)}")

Output

Enter a positive number -> 2
2 in binary is 0b10
2 in octal is 0o2
2 in hexadecimal is 0x2
Enter a positive number -> 8
8 in binary is 0b1000
8 in octal is 0o10
8 in hexadecimal is 0x8
Enter a positive number -> 756
756 in binary is 0b1011110100
756 in octal is 0o1364
756 in hexadecimal is 0x2f4

We have simply used:

  • bin() to convert decimal to binary.
  • oct() to convert octal to binary.
  • hex() to convert hexadecimal to binary.

Check out our other python programming examples

Leave a Comment

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