# Binary to decimal and vice-versa in python

## Binary to decimal and vice-versa in python

Write Python code for converting a decimal number to it’s binary equivalent and vice-versa.

Example:

```From decimal to binary
Input : 8
Output : 1 0 0 0

From binary to decimal
Input : 100
Output : 4```

Decimal to binary

```Keep calling conversion function with n/2  till n > 1,
later perform n % 1 to get MSB of converted binary number.
Example :- 7
1). 7/2 = Quotient = 3(greater than 1), Remainder = 1.
2). 3/2 = Quotient = 1(not greater than 1), Remainder = 1.
3). 1%2 = Remainder = 1.
 `# Function to print binary number for the` `# input decimal using recursion` `def` `decimalToBinary(n):` `    ``if``(n > ``1``):` `        ``# divide with integral result` `        ``# (discard remainder)` `        ``decimalToBinary(n``/``/``2``)` `    ` `    ``print``(n``%``2``, end``=``' '``)` `    ` `    ` `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``decimalToBinary(``8``)` `    ``print``(``"\n"``)` `    ``decimalToBinary(``18``)` `    ``print``(``"\n"``)` `    ``decimalToBinary(``7``)` `    ``print``(``"\n"``)`

Output:

```1 0 0 0
1 0 0 1 0
1 1 1```

Decimal to binary using bin():

 `# Function to convert Decimal number` `# to Binary number` `def` `decimalToBinary(n):` `    ``return` `bin``(n).replace(``"0b"``,"")` `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``print``(decimalToBinary(``8``))` `    ``print``(decimalToBinary(``18``))` `    ``print``(decimalToBinary(``7``))`

Output:

```1000
10010
111```

Binary to decimal

```Example -: 1011
1). Take modulo of given binary number with 10.
(1011 % 10 = 1)
2). Multiply rem with 2 raised to the power
it's position from right end.
(1 * 2^0)
Note that we start counting position with 0.
3). Add result with previously generated result.
decimal = decimal + (1 * 2^0)
4). Update binary number by dividing it by 10.
(1011 / 10 = 101)
5). Keep repeating upper steps till binary > 0.

Final Conversion -: (1 * 2^3) + (0 * 2^2) +
(1 * 2^1) + (1 * 2^0) = 11```
 `# Function calculates the decimal equivalent` `# to given binary number` `def` `binaryToDecimal(binary):` `    ` `    ``binary1 ``=` `binary` `    ``decimal, i, n ``=` `0``, ``0``, ``0` `    ``while``(binary !``=` `0``):` `        ``dec ``=` `binary ``%` `10` `        ``decimal ``=` `decimal ``+` `dec ``*` `pow``(``2``, i)` `        ``binary ``=` `binary``/``/``10` `        ``i ``+``=` `1` `    ``print``(decimal)   ` `    ` `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``binaryToDecimal(``100``)` `    ``binaryToDecimal(``101``)` `    ``binaryToDecimal(``1001``)`

Output:

```4
5
9```

Binary to decimal using int():

 `# Function to convert Binary number` `# to Decimal number` `def` `binaryToDecimal(n):` `    ``return` `int``(n,``2``)` `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``print``(binaryToDecimal(``'100'``))` `    ``print``(binaryToDecimal(``'101'``))` `    ``print``(binaryToDecimal(``'1001'``))`

Output:

```4
5
9```

Last Updated on October 28, 2021 by admin

## Python – grid() method in TkinterPython – grid() method in Tkinter

Python | grid() method in Tkinter The Grid geometry manager puts the widgets in a 2-dimensional table.

## How to pass multiple arguments to function ?How to pass multiple arguments to function ?

How to pass multiple arguments to function ? A Routine is a named group of instructions performing

## Python – Difference between two listsPython – Difference between two lists

Python – Difference between two lists   There are various ways in which the difference

## Python Set difference() MethodPython Set difference() Method

Python Set | difference() The difference between the two sets in Python is equal to

## How to Convert integer to string in PythonHow to Convert integer to string in Python

In Python an integer can be converted into a string using the built-in str() function.

## Python Dictionary keys() methodPython Dictionary keys() method

Python Dictionary keys() method Dictionary in Python is a collection of data values which only

## Dunder or magic methods in PythonDunder or magic methods in Python

Dunder or magic methods in Python Dunder or magic methods in Python are the methods having two

## Python – Remove punctuation from stringPython – Remove punctuation from string

Python | Remove punctuation from string Many times while working with Python strings, we have