Write a Python program to convert a binary number to decimal number


This program is written in Python and it converts a binary number to its equivalent decimal number. The program first prompts the user to enter the binary number using the input() function and stores it as a list of characters using the list() function. It then initializes a variable val to zero, which will hold the decimal value of the binary number.

The program then uses a for loop to iterate over each binary digit in the list. In each iteration, it pops the last binary digit from the list using the pop() method and assigns it to the variable digit.

The program checks if the digit is equal to '1' using an if statement. If it is, the program calculates the decimal value of that binary digit using the pow() function and adds it to the variable val. The pow() function calculates the power of 2 for each position of the binary digit, starting from 2^0 for the rightmost digit and increasing by 1 for each subsequent digit.

After iterating over all the binary digits, the program prints the decimal value of the binary number using the print() function. Overall, this program provides a simple implementation of how to convert a binary number to its equivalent decimal number and can be used to quickly convert binary numbers to decimal numbers in Python.

Source Code

bin = list(input("Enter the Binary Number : "))
val = 0
 
for i in range(len(bin)):
	digit = bin.pop()
	if digit == '1':
		val +=  pow(2, i)
 
print("Convert Binary to Decimal Number is :", val)

Output

Enter the Binary Number : 100100
Convert Binary to Decimal Number is : 36

Example Programs