A Program to Convert Decimal to Binary, Octal and Hexadecimal

In this example, we are giving a program to convert decimal to binary, octal and hexadecimal.


Program to convert decimal to binary, octal and hexadecimal:

  1. # Convert decimal to binary, octal and hexadecimal
  2. dec = int(input('Enter decimal number: '))
  3. print("The decimal value of", dec, "is:")
  4. print(bin(dec), "in binary.")
  5. print(oct(dec), "in octal.")
  6. print(hex(dec), "in hexadecimal.")

Output:

Enter decimal number: 10
The decimal value of 10 is:
0b1010 in binary.
0o12 in octal.
0xa in hexadecimal.