In the previous article, we have discussed Python Program to Convert Octal to Decimal using While Loop
Given a binary number and the task is to get the respective octal number using the while loop.
Examples:
Example1:
Input:
Given Binary Number = 1010
Output:
The Octal value of the given binary number { 1010 } is: 12
Example2:
Input:
Given Binary Number = 1101
Output:
The Octal value of the given binary number { 1101 } is: 15
Program to Convert Binary to Octal using While Loop in Python
Below are the ways to convert the given binary number into octal :
Method #1: Using While Loop (Static Input)
Approach:
- Give the binary number as static input and store it in a variable.
- Take a variable say octl_num and initialize its value with 0.
- Take another variable say deciml_num and initialize its value with 0.
- Again take another variable say ‘a’ and initialize its value with 0.
- Loop till the given binary number is not equal to zero using the while loop.
- Inside the loop, calculate the given binary number modulus 10 (to get the last digit).
- Store it in a variable ‘b‘.
- Calculate the value of 2 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 binary number by 10 and store it in the same variable binry_numb.
- 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 8 and store it in a variable ‘d’.
- Add the value of ‘d’ with the above-initialized octl_num and store it in the same variable octl_num.
- Divide the deciml_num by 8 and store it in the same variable deciml_num.
- Multiply ‘a’ with 10 and store it in the same variable ‘a’.
- Print the octl_num to get the octal value of the given binary number.
- The Exit of the Program.
Below is the implementation:
# Give the binary number as static input and store it in a variable. binry_numb = 1010 # Take a variable say octl_num and initialize its value with 0. octl_num = 0 # Take another variable say deciml_num and initialize its value with 0. deciml_num = 0 # Again take another variable say 'a' and initialize its value with 0. a = 0 # Loop till the given binary number is not equal to zero using the while loop. print("The Octal value of the given binary number {", binry_numb, "} is: ") while (binry_numb != 0): # Inside the loop, calculate the given binary number modulus 10 (to get the last digit). # store it in a variable 'b'. b = (binry_numb % 10) # Calculate the value of 2 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(2, 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 binary number by 10 and store it in the same variable binry_numb. binry_numb = binry_numb // 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 8 and store it in a variable 'd'. d = (deciml_num % 8) * a # Add the value of 'd' with the above-initialized octl_num and store it in the same # variable octl_num. octl_num = octl_num + d # Divide the deciml_num by 8 and store it in the same variable deciml_num. deciml_num = deciml_num // 8 # Multiply 'a' with 10 and store it in the same variable 'a'. a = a * 10 # Print the octl_num to get the octal value of the given binary number. print(octl_num)
Output:
The Octal value of the given binary number { 1010 } is: 12
Method #2: Using While loop (User Input)
Approach:
- Give the binary number as user input using the int(input()) function and store it in a variable.
- Take a variable say octl_num and initialize its value with 0.
- Take another variable say deciml_num and initialize its value with 0.
- Again take another variable say ‘a’ and initialize its value with 0.
- Loop till the given binary number is not equal to zero using the while loop.
- Inside the loop, calculate the given binary number modulus 10 (to get the last digit).
- Store it in a variable ‘b‘.
- Calculate the value of 2 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 binary number by 10 and store it in the same variable binry_numb.
- 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 8 and store it in a variable ‘d’.
- Add the value of ‘d’ with the above-initialized octl_num and store it in the same variable octl_num.
- Divide the deciml_num by 8 and store it in the same variable deciml_num.
- Multiply ‘a’ with 10 and store it in the same variable ‘a’.
- Print the octl_num to get the octal value of the given binary number.
- The Exit of the Program.
Below is the implementation:
# Give the binary number as user input using the int(input()) function and store it # in a variable. binry_numb = int(input("Enter some random Number = ")) # Take a variable say octl_num and initialize its value with 0. octl_num = 0 # Take another variable say deciml_num and initialize its value with 0. deciml_num = 0 # Again take another variable say 'a' and initialize its value with 0. a = 0 # Loop till the given binary number is not equal to zero using the while loop. print("The Octal value of the given binary number {", binry_numb, "} is: ") while (binry_numb != 0): # Inside the loop, calculate the given binary number modulus 10 (to get the last digit). # store it in a variable 'b'. b = (binry_numb % 10) # Calculate the value of 2 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(2, 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 binary number by 10 and store it in the same variable binry_numb. binry_numb = binry_numb // 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 8 and store it in a variable 'd'. d = (deciml_num % 8) * a # Add the value of 'd' with the above-initialized octl_num and store it in the same # variable octl_num. octl_num = octl_num + d # Divide the deciml_num by 8 and store it in the same variable deciml_num. deciml_num = deciml_num // 8 # Multiply 'a' with 10 and store it in the same variable 'a'. a = a * 10 # Print the octl_num to get the octal value of the given binary number. print(octl_num)
Output:
Enter some random Number = 1101 The Octal value of the given binary number { 1101 } is: 15
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.