# Python | Convert the binary number to decimal without using library function

In this article, we will be covering all the methods that we have to solve the given problem

## Method 1: Using a while loop

```print("Enter the Binary Number that you want to concver to Decimal: ")
binary_num = int(input())

dnum = 0
i = 1
while binary_num!=0:
rem = binary_num%10
dnum = dnum + (rem*i)
i = i*2
binary_num = int(binary_num/10)

print("\nThe decimal value for the entered binary number is = ", dnum)```

Output:

``````Enter the Binary Number that you want to concver to Decimal:
10110

The decimal value for the entered binary number is =  22

Enter the Binary Number that you want to concver to Decimal:
11110110

The decimal value for the entered binary number is =  246``````

Explanation:

Here we defined two variables namely “dnum” and “i”, we then run a while loop until the binary_num gets equal to zero. We then stored the remainder of binary_num % 10 in the rem variable. In the next line, we added the dnum with the value computed by rem*i. We then change the value of i by multiplying it by 2 in each iteration and finally assign the integer value that is computed by dividing the binary_num by 10

## Method 2: Using the int() function

```print("Enter the Binary Number that you want to concver to Decimal: ", end="")
binary_num = input()

dnum = int(binary_num, 2)
print("\nThe decimal value for the entered binary number is = ", dnum)```

Explanation:

In this approach, we simply used the int function. There are two parameters for the Python function int(). These two justifications are (1) x- Create an integer object from a number or string. The x parameter’s default value is zero. Base: A number serves as a representation of the number format. By default, the base is set at 10. This argument is not required.

## Method 3: Using the user-defined function

```binary = input("Enter the Binary Number that you want to concver to Decimal: ")
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print("\nThe decimal value for the entered binary number is = ", decimal)```

Explanation:

Here we defined a function in which we run the for loop on the digits in the binary numbers entered by the user. We convert that digit in the binary number to an integer using the int() function and then add it to the decimal * 2. Now this computed value is stored in decimal and in the next iteration of the loop, we use this decimal value.

## Method 4: Using the class

```class binaryTODecimal:
def bin_to_dec(self, b):
return int(b, 2)

print("Enter the Binary Number that you want to concver to Decimal: ", end="")
binary_num = input()

ob = binaryTODecimal()
dnum = ob.bin_to_dec(binary_num)
print("\nThe decimal value for the entered binary number is = , dnum)```

Explanation:

To access the bin_to_dec() member function of the class binaryTODecimal, an object called ob is created and assigned the dot (.) operator. The rest of the equipment functions normally.

## Method 5: Using a separate function for each operation

```def compute_degree(number):
power = 1

while number % (10**power) != number:
power += 1

return power

def compute_digits(number):
digits = []
degree = compute_degree(number)

for x in range(0, degree):
digits.append(int(((number % (10**(degree-x))) - (number % (10**(degree-x-1)))) / (10**(degree-x-1))))
return digits

def bin_to_dec(number):
list = compute_digits(number)
dec_val = 0
for x in range(0, len(list)):
if (list[x] is 1):
dec_val += 2**(len(list) - x - 1)
return dec_val

bin_num = int(input("Enter the Binary Number that you want to concver to Decimal: "))

print(bin_to_dec(bin_num))```

Output:

``````Enter a binary number: 110111110
446``````

Here is the end of this article. We tried to cover different methods in order to showcase various concepts involved in different forms. 