by @kodeazy

How to convert binary to decimal number without using pre-defined function using while loop in python?

Home » python » How to convert binary to decimal number without using pre-defined function using while loop in python?
  • In this blog we will discuss on how to convert binary number to decimal number in python.

    For Example

    Input : 1 0 1 0
    Output : 10

    Example program to convert Binary to Decimal

    import math
    binaryNum = input("Enter the binary digit to convert into decimal Number:")
    i_binary = int(float(binaryNum))
    f_binary=i_binary
    index=0
    sum=0
    while i_binary!=0:
    s1=int(i_binary%10);
    i_binary=int(i_binary/10);
    sum=sum+(s1*(math.pow(2, index)));
    index=index+1;
    xx1=float(binaryNum)-int(float(binaryNum));
    st=str(xx1);
    count=0
    j=-1
    sum1=0
    for index in st:
    if count>1:
    	sum1=sum1+(int(index)*(math.pow(2, j)));
    	j=j-1
    count=count+1
    sum=sum+sum1
    print(sum)

    Output:

    Enter the binary digit to convert into decimal Number:1010
    10.0