Python Examples

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

We all know how to convert a number from decimal to octal, binary, and hexadecimal, as we would have solved many mathematical problems based on them. Now, you will learn how to do these conversions using the Python programming language. 

Decimal to Binary 

First, we will see how to convert a number from decimal to binary. We can convert a number from decimal to binary using different methods, which are: 

Using Recursion 

                    

def DecimalToBinary(number):
     

    if num >= 1:

        DecimalToBinary(number // 2)

    print(number % 2, end = '')


# Driver Code

if __name__ == '__main__':


    # decimal value

    decimal_val = 17


    # Calling function

    DecimalToBinary(decimal_val)

Output:

Using In-Built Function 

                    

def decimalToBinary(num):

    return bin(num).replace("0b", "")

 
# Driver code

if __name__ == '__main__':

    print(decimalToBinary(10))

    print(decimalToBinary(24))

    print(decimalToBinary(9))

Output:

Without Built-in function 

                    

def decimalToBinary(num):

    return "{0:b}".format(int(num))


# Driver code

if __name__ == '__main__':

    print(decimalToBinary(10))

    print(decimalToBinary(24))

    print(decimalToBinary(9))

 Output: 

Decimal to Octal 

Now, let us see how we can convert a number from decimal to octal using the Python programming language. First let us write down the algorithm before solving the problem, which helps us write the code easily. 

Algorithm

  1. After dividing the number by 8, we store the remainder in an array
  2. Divide the number by 8 now
  3. Repeat the above two steps until the number becomes equal to zero
  4. Now, print the array in reverse order 

Python_Code

                    

def decToOctal(num):

 

    # array to store

    # octal number

    octalNumber = [0] * 100

 

    # counter for octal

    # number array

    i = 0

    while (num!= 0):

 

        # storing remainder

        # in octal array

        octalNumber[i] = num % 8

        num = int(num / 8)

        i += 1

 

    # printing octal number

    # array in reverse order

    for k in range(i - 1, -1, -1):

        print(octalNumber[k], end="")

# Driver Code

num = 33

# Function Call

decToOctal(num)

 Output: 

Decimal to Hexadecimal 

Now. Let us see how we can convert a number from decimal to hexadecimal using the Python programming language. 

Algorithm 

  1. After dividing the number by 16, store the remainder in a temporary variable temp1. 
  2. If the temp1<10, insert( 48+temp1) into the array otherwise insert (55+temp1) into the array. 
  3. Now, divide the number by 16  
  4. Repeat the above steps until the number becomes equal to zero 
  5. Now, print the array in reverse order 

Code: 

                    

def decToHexa(num):


    # char array to store

    # hexadecimal number

    hexaDeciNumber = ['0'] * 100


    # counter for hexadecimal

    # number array

    I = 0

    while(num!= 0):

 

        # temporary variable

        # to store remainder

        temp = 0

 

        # storing remainder

        # in temp variable.

        temp = num % 16

 

        # check if temp < 10

        if(temp < 10):

            hexaDeciNumber[i] = chr(temp + 48)

            i = i + 1

        else:

            hexaDeciNumber[i] = chr(temp + 55)

            i = i + 1

        num = int(num / 16)

 

    # printing hexadecimal number

    # array in reverse order

    j = i - 1

    while(j >= 0):

        print((hexaDeciNumber[j]), end="")

        j = j - 1

# Driver Code

num = 2545

decToHexa(num)

Output:

Q&A on Decimal to Octal

Now, to give you a quick brief on how these conversions work, we have answered some frequently answered questions for you, which are: 

Q1. How do you convert from decimal to octal?

A. We can convert from octal to decimal by following these steps:

                    

def octalToDecimal(num):
 
    number = n
    dec_value = 0
 
    # Initializing base value
    # to 1, i.e 8^0
    base = 1
 
    temp = number
    while (temp):
 
        # Extracting last digit
        last_digit = temp % 10
        temp = int(temp / 10)
 
        # Multiplying last digit
        # with appropriate base
        # value and adding it
        # to dec_value
        dec_value += last_digit * base
 
        base = base * 8
 
    return dec_value
 
# Driver Code
number = 67
print(octalToDecimal(number))

This code gives the output in octal number: 55
 
Q2. What is the decimal of octal 177?
 
A. The decimal value of the octal number 177 is 127.
(177)8 = (127)10
 
Q3. What is 10101 as a decimal?
 
A. The decimal equivalent of 10101 is 21.
 
Q4. How do you convert decimal to octal and hexadecimal?
 
A. We can convert decimal to octal by following these steps:
  1. After dividing the number by 8, we store the remainder in an array
  2. Divide the number by 8 now
  3. Repeat the above two steps until the number becomes equal to zero
  4. Now, print the array in reverse order 

We can convert decimal to hexadecimal by following these steps:

  1. After dividing the number by 16, store the remainder in a temporary variable temp1. 
  2. If the temp1<10, insert( 48+temp1) into the array otherwise insert (55+temp1) into the array. 
  3. Now, divide the number by 16  
  4. Repeat the above steps until the number becomes equal to zero 
  5. Now, print the array in reverse order
Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.