Python Program to Convert Binary Number to Octal and vice-versa

In this python tutorial, you will learn how to Convert Binary Number to Octal and vice-versa with the built in functions of the python programming language.

How to Convert Binary Number to Octal and vice-versa?

Let’s take a look at the first source code,  here the values are given as input by the user in the code, the built in functions carries out the function.

BINARY TO OCTAL 

RUN CODE SNIPPET
#Python Program to Convert Binary number to Octal

print("Enter a Binary Number: ", end="")
bnum = input()

onum = int(bnum, 2)
onum = oct(onum)

print("\nEquivalent Octal Value = ", onum[2:])

INPUT:

 110011101

OUTPUT:

Enter a Binary Number: 
Equivalent Octal Value = 635
  1. We declare a print function to display the statement ("Enter a Binary Number: ", end="")after which we declare the variable bnum = input(), where we give the user the option to enter the values and the input values are scanned using the input function
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the variable onum with the  function int(bnum, 2) where the int function converts the string to an integer type value with base two, that is it converts it into a binary number.
  4. We declare the variable onum with the  function oct(onum) where the oct function converts the value of onum to its equivalent octal value.
  5. Now, we display the output value using the print and display the statement/string ("\nEquivalent Octal Value = ", onum[2:]) . The function [2:] is added to skip the first two characters of the obtained octal number.

Let’s take a look at the second source code,  here the values are given as input by the user in the code, the built in functions carries out the function.

OCTAL TO BINARY

RUN CODE SNIPPET
#Python Program to Convert Octal number to Binary

print("Enter Octal Number: ", end="")
onum = input()

bnum = int(onum, 8)
bnum = bin(bnum)

print("\nEquivalent Binary Value =", bnum[2:])

INPUT:

635

OUTPUT:

Enter Octal Number: 
Equivalent Binary Value = 110011101
  1. We declare a print function to display the statement ("Enter Octal Number: ", end="")after which we declare the variable onum = input(), where we give the user the option to enter the values and the input values are scanned using the input function
  2. In the STDIN section of the code editor the input values are entered.
  3. We declare the variable bnum which is equal to the function int(onum, 8), where the int convert a value passed as its argument to its integer equivalent.
  4. We declare the variable bnum which is equal to the function bin(bnum), where the bin returns binary equivalent of value of he stored value.
  5. Now, we display the output value using the print and display the statement/string ("\nEquivalent Binary Value =", bnum[2:]) . The function [2:] is added to skip the first two characters of the obtained binary number.

NOTE:

  • The End parameter is used to add any string at the end of the output of the print statement
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  •  The oct() function is used to get an octal value of an integer number.
  •  The int() function is used to get the decimal value of the given integer.
  •  The bin() function is used to get the Binary value of the given integer
  • The print() function prints the specified message to the screen, or other standard output device.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this python tutorial, you will learn how to Display Prime Numbers Between Two Intervals using the if and else...
In this python tutorial, you will learn how to Calculate Standard Deviation with built in functions of the python programming...
In this Python program, we will convert temperature values from Celsius to Fahrenheit. The Celsius and Fahrenheit scales are two...