Binary to Decimal Python

PROGRAM TO CONVERT BINARY NUMBER TO DECIMAL NUMBER


def binaryToDecimal(n):
    num = n;
    dec_value = 0;
      
    # Initializing base 
    # value to 1, i.e 2 ^ 0
    base1 = 1;
      
    len1 = len(num);
    for i in range(len1 - 1, -1, -1):
        if (num[i] == '1'):     
            dec_value += base1;
        base1 = base1 * 2;
      
    return dec_value;
  
# Driver Code
num = "10101001"
print(binaryToDecimal(num));


OUTPUT
169

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java