Program to Convert Decimal to Binary, Octal and Hexadecimal

Python Program to Convert Decimal to Binary, Octal and Hexadecimal

Decimal System: The decimal system is the most extensively used number system. This is a base ten numbering system. A number is represented by 10 numbers (0-9) in this system.

Binary Number System: The binary number system is a base two number system. Because computers can only understand binary numbers, the binary system is utilized (0 and 1).

Octal system: Octal system is a base 8 numbering system.

Hexadecimal Number System: The Hexadecimal number system is a base 16 number system.

The decimal system is the most extensively used system of numbering. Computers, on the other hand, only understand binary. We may need to translate decimal into binary, octal, or hexadecimal number systems because they are closely connected.

The decimal system is based on base ten (ten symbols, 0-9, are used to represent a number), while binary is based on base 2, octal is based on base 8, and hexadecimal is based on base 16.

Binary numbers have the prefix 0b, octal numbers have the prefix 0o, and hexadecimal numbers have the prefix 0x.

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

Examples:

Example1:

Input:

Decimal_number=1028

Output:

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:
The binary value of the given decimal number 1028 = 0b10000000100
The octal value of the given decimal number 1028 = 0o2004
The hexadecimal value of the given decimal number 1028 = 0x404

Example2:

Input:

Decimal_number=937238

Output:

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:
The binary value of the given decimal number 937238 = 0b11100100110100010110
The octal value of the given decimal number 937238 = 0o3446426
The hexadecimal value of the given decimal number 937238 = 0xe4d16

Example3:

Input:

Decimal_number=0

Output:

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:
The binary value of the given decimal number 0 = 0b0
The octal value of the given decimal number 0 = 0o0
The hexadecimal value of the given decimal number 0 = 0x0

Program to Convert Decimal to Binary, Octal and Hexadecimal in Python

Python provides different types of built in functions to convert into different number systems like

  1. bin() : To convert the given decimal number to binary number system
  2. hex() : To convert the given decimal number to binary number system
  3. oct()  :To convert the given decimal number to binary number system

We will use the above mentioned functions to convert the given decimal number into binary,octal,hexadecimal number system.

Below is the implementation:

# given number in decimal number system
decimal_Number = 1028
# converting the given decimal_number to binary_number system
# we use bin() function to convert given decimal_number to binary_number system
binValue = bin(decimal_Number)
# converting the given decimal_number to octal_number system
# we use bin() function to convert given decimal_number to binary_number system
octalValue = oct(decimal_Number)
# converting the given decimal_number to hexadecimal_number system
# we use bin() function to convert given decimal_number to binary_number system
hexValue = hex(decimal_Number)
# printing the values after the conversion of bases that is
# decimal to binary,octal,hexadecimal
print("Printing the values after the conversion of bases"
      " that is decimal to binary,octal,hexadecimal:")
# printing the binary converted value
print("The binary value of the given decimal number",
      decimal_Number, "=", binValue)
# printing the octal converted value
print("The octal value of the given decimal number",
      decimal_Number, "=", octalValue)
# printing the hexadecimal converted value
print("The hexadecimal value of the given decimal number",
      decimal_Number, "=", hexValue)

Output:

Printing the values after the conversion of bases that is decimal to binary,octal,hexadecimal:
The binary value of the given decimal number 1028 = 0b10000000100
The octal value of the given decimal number 1028 = 0o2004
The hexadecimal value of the given decimal number 1028 = 0b10000000100

Related Programs: