Program to Convert Octal to Binary

Python Program to Convert Octal to Binary

In the previous article, we have discussed Python Program to Find the Position of an Element in a Matrix
Given an octal number and the task is to get the respective binary number using the while loop.

Examples:

Example1:

Input:

Given Octal Number = 10

Output:

The Binary value of the given Octal number { 10 } is: 
1000

Example2:

Input:

Given Octal Number = 35

Output:

The Binary value of the given Octal number { 35 } is: 
11101

Program to Convert Octal to Binary in Python

Below are the ways to convert the given octal number into binary :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the octal number as static input and store it in a variable.
  • Take a variable say ‘a ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Again take another variable say ‘binry_num’ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
  • Store it in a variable ‘b‘.
  • Calculate the value of 8 raised to the power ‘a‘ using the pow() function and multiply it with the above-obtained value ‘b‘.
  • Store it in a variable ‘c‘.
  • Add ‘c‘ to the above-initialized deciml_num and store it in the same variable deciml_num.
  • Increment the value a by 1 and store it in the same variable ‘a’.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Come out of the while loop and initialize the value of ‘a’ with 1.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Multiply  ‘a‘ with the deciml_num modulus 2 and store it in a variable ‘d‘.
  • Add the value of ‘d’ with the above-initialized binry_num and store it in the same variable binry_num.
  • Divide the deciml_num by 2 and store it in the same variable deciml_num.
  • Multiply ‘a’ with 10 and store it in the same variable ‘a’.
  • Print the binry_num to get the binary value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as static input and store it in a variable.
gvn_octl_num = 10
# Take a variable say 'a 'and initialize its value with 0.
a = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Again take another variable say 'binry_num' and initialize its value with 0.
binry_num = 0
# Loop till the given octal number is not equal to zero using the while loop.
print("The Binary value of the given Octal number {", gvn_octl_num, "} is: ")
while (gvn_octl_num != 0):
  # Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
    # Store it in a variable 'b'.
    b = (gvn_octl_num % 10)
  # Calculate the value of 8 raised to the power 'a' using the pow() function and multiply
# it with the above-obtained value 'b'.
# Store it in a variable 'c'.
    c = pow(8, a)*b
  # Add 'c' to the above-initialized deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num + c
  # Increment the value a by 1 and store it in the same variable 'a'.
    a += 1
   # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
 # Come out of the while loop and initialize the value of 'a' with 1.
a = 1
# Loop till the decimal number is not equal to zero using the while loop.
while (deciml_num != 0):
  # Multiply  'a' with the deciml_num modulus 2 and store it in a variable 'd'.
    d = (deciml_num % 2) * a
  # Add the value of 'd' with the above-initialized binry_num and store it in the same
# variable binry_num.
    binry_num = binry_num + d
# Divide the deciml_num by 2 and store it in the same variable deciml_num.
    deciml_num = deciml_num // 2
# Multiply 'a' with 10 and store it in the same variable 'a'.
    a = a * 10
 # Print the binry_num to get the binary value of the given octal number.
print(binry_num)

Output:

The Binary value of the given Octal number { 10 } is: 
1000

Method #2: Using While loop (User Input)

Approach:

  • Give the octal number as user input using the int(input()) function and store it in a variable.
  • Take a variable say ‘a ‘and initialize its value with 0.
  • Take another variable say deciml_num and initialize its value with 0.
  • Again take another variable say ‘binry_num’ and initialize its value with 0.
  • Loop till the given octal number is not equal to zero using the while loop.
  • Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
  • Store it in a variable ‘b‘.
  • Calculate the value of 8 raised to the power ‘a‘ using the pow() function and multiply it with the above-obtained value ‘b‘.
  • Store it in a variable ‘c‘.
  • Add ‘c‘ to the above-initialized deciml_num and store it in the same variable deciml_num.
  • Increment the value a by 1 and store it in the same variable ‘a’.
  • Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
  • Come out of the while loop and initialize the value of ‘a’ with 1.
  • Loop till the decimal number is not equal to zero using the while loop.
  • Multiply  ‘a‘ with the deciml_num modulus 2 and store it in a variable ‘d‘.
  • Add the value of ‘d’ with the above-initialized binry_num and store it in the same variable binry_num.
  • Divide the deciml_num by 2 and store it in the same variable deciml_num.
  • Multiply ‘a’ with 10 and store it in the same variable ‘a’.
  • Print the binry_num to get the binary value of the given octal number.
  • The Exit of the Program.

Below is the implementation:

# Give the octal number as user input using the int(input()) function 
# and store it in a variable.
gvn_octl_num = int(input("Enter some Random Number = "))
# Take a variable say 'a 'and initialize its value with 0.
a = 0
# Take another variable say deciml_num and initialize its value with 0.
deciml_num = 0
# Again take another variable say 'binry_num' and initialize its value with 0.
binry_num = 0
# Loop till the given octal number is not equal to zero using the while loop.
print("The Binary value of the given Octal number {", gvn_octl_num, "} is: ")
while (gvn_octl_num != 0):
  # Inside the loop, calculate the given octal number modulus 10 (to get the last digit).
    # Store it in a variable 'b'.
    b = (gvn_octl_num % 10)
  # Calculate the value of 8 raised to the power 'a' using the pow() function and multiply
# it with the above-obtained value 'b'.
# Store it in a variable 'c'.
    c = pow(8, a)*b
  # Add 'c' to the above-initialized deciml_num and store it in the same variable deciml_num.
    deciml_num = deciml_num + c
  # Increment the value a by 1 and store it in the same variable 'a'.
    a += 1
   # Divide the given octal number by 10 and store it in the same variable gvn_octl_num.
    gvn_octl_num = gvn_octl_num // 10
 # Come out of the while loop and initialize the value of 'a' with 1.
a = 1
# Loop till the decimal number is not equal to zero using the while loop.
while (deciml_num != 0):
  # Multiply  'a' with the deciml_num modulus 2 and store it in a variable 'd'.
    d = (deciml_num % 2) * a
  # Add the value of 'd' with the above-initialized binry_num and store it in the same
# variable binry_num.
    binry_num = binry_num + d
# Divide the deciml_num by 2 and store it in the same variable deciml_num.
    deciml_num = deciml_num // 2
# Multiply 'a' with 10 and store it in the same variable 'a'.
    a = a * 10
 # Print the binry_num to get the binary value of the given octal number.
print(binry_num)

Output:

Enter some Random Number = 35
The Binary value of the given Octal number { 35 } is: 
11101

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.