Binary to Decimal

    0

    2

    Mahendra Kumar

    beginnerbasicMathsfunction

    Convert Binary to Decimal in Python using while loop

    def BinaryDecimal(n):  #user-defined function
        num, dec, base = n, 0, 1
        temp = num
        while(temp):
            digit = temp % 10
            temp = int(temp / 10)
            dec += digit * base
            base = base * 2
        return dec
    # take inputs
    num = int(input('Enter a binary number: '))
    # display result
    print('The decimal value is =', BinaryDecimal(num))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.