Software & Finance





Python - Decimal to Binary Conversion





 

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

 

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