Algorithm


  1. Input:

    • Take a decimal number as input.
    • Conversion to Binary: Initialize an empty string to store the binary representation.
      • Use a loop to repeatedly divide the decimal number by 2.
      • Append the remainder to the binary string.
      • Update the decimal number to the quotient.
      • Repeat until the decimal number becomes 0.
      • Reverse the binary string to get the final binary representation.

        Conversion to Octal:

        • Initialize an empty string to store the octal representation.
        • Use a loop to repeatedly divide the decimal number by 8.
        • Append the remainder to the octal string.
        • Update the decimal number to the quotient.
        • Repeat until the decimal number becomes 0.
        • Reverse the octal string to get the final octal representation.

          Conversion to Hexadecimal:

          • Initialize an empty string to store the hexadecimal representation.
          • Use a loop to repeatedly divide the decimal number by 16.
          • Convert the remainder to its hexadecimal equivalent (0-9, A-F).
          • Append the hexadecimal digit to the string.
          • Update the decimal number to the quotient.
          • Repeat until the decimal number becomes 0.
          • Reverse the hexadecimal string to get the final hexadecimal representation.

            Output:

            • Display the binary, octal, and hexadecimal representations.

 

Code Examples

#1 Example- Decimal to Binary -Using Recursion

Code - Python Programming

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)
Copy The Code & Try With Live Editor

Output

x
+
cmd
10001

#2 Example- Decimal to Binary-Using In-Built Function

Code - Python Programming

def decimalToBinary(num):

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

 
# Driver code

if __name__ == '__main__':

    print(decimalToBinary(10))

    print(decimalToBinary(24))

    print(decimalToBinary(9))
Copy The Code & Try With Live Editor

Output

x
+
cmd
1010
11000
1001

#3 Example- Decimal to Binary-Without Built-in function

Code - Python Programming

def decimalToBinary(num):

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


# Driver code

if __name__ == '__main__':

    print(decimalToBinary(10))

    print(decimalToBinary(24))

    print(decimalToBinary(9))
Copy The Code & Try With Live Editor

Output

x
+
cmd
1010
11000
1001

#4 Example- Decimal to Octal

Code - Python Programming

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)

                
Copy The Code & Try With Live Editor

Output

x
+
cmd
41

#5 Example- Decimal to Hexadecimal

Code - Python Programming

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)
Copy The Code & Try With Live Editor

Output

x
+
cmd
41

#6 We can convert from octal to decimal by following these steps:

Code - Python Programming

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))

                
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Python Programing Example to Convert Decimal to Binary, Octal and Hexadecimal-DevsEnv