Software & Finance





Python - Binary to Decimal Conversion





 

Here is the python source code to convert a binary number to decimal number. This code is tested with Python version 2.6

 

        print "Program for Binary to Decimal Conversion"
        
        dec = 0
        bin = 0
        factor = 1;
        
        print "Enter Binary Number:", 
        bin = input()
        
        while(bin > 0):
           if( (bin % 10) == 1):
              dec += factor
           bin /= 10
           factor = factor * 2
            
        print "The Decimal Number is: ", dec
        
        #sample output
        #Program for Binary to Decimal Conversion
        #Enter Binary Number: 1010
        #The Decimal Number is: 10