Write a program to find the binary number from given decimal number


This program takes in a binary number as input from the user, then it uses a for loop to iterate through each digit in the binary number. The variable decimal is initialized to 0 and the value of decimal is then multiplied by 2 and the current digit of the binary number is added to it. This process is repeated for all digits in the binary number. Finally, the program prints out the original binary number as well as its equivalent decimal number. The program converts binary number to decimal number using simple mathematical calculations.

Source Code

"""
num = input("Enter the Binary Number :")
dec_number= int(num, 2)
print('The binary Number :', num)
print('The decimal Number :', dec_number)
"""
binary = input("Enter the Binary Number :")
decimal = 0
for digit in binary:
    decimal = decimal*2 + int(digit)
print('The binary Number :', binary)
print('The decimal Number :', decimal)

Output

Enter the Binary Number :1010
The binary Number : 1010
The decimal Number : 10

Example Programs