Learn Programming and technology with ease @ developerpublish.com

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

# 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Â

```#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

```#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.

### You May Also Like

#### Python Program to Remove Duplicates from a Linked List

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...

#### Python Program to Solve the Celebrity Problem

This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...

#### Python Program to Solve n-Queen Problem with Recursion

This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...