- 1

Write a python program to convert a given binary number into its equivalent decimal.

Test cases: Input: 110101  Output: 53 # Python3 program to convert # binary to decimal   # Function to convert # binary to decimal def binaryToDecimal(n):     num = n;     dec_value = 0;           # Initializing base     # value to 1, i.e 2 ^ 0     base = 1;           temp = num;     while(temp):         last_digit = temp % 10;         temp = int(temp / 10);                   dec_value += last_digit * base;         base = base * 2;     return dec_value;   # Driver Code num = 10101001; print(binaryToDecimal(num));   # This code is contributed

13th Jun 2021, 5:38 AM
Madhava Raj B
Madhava Raj B - avatar
1 Antwort
+ 3
the code you've posted works fine if you remove all invisible illegal characters: I guess that's not your attempt, but a ready made solution wich you have copy-pasted from somewhere on internet by beeing too lazy to copy by typing it ;P however, it doesn't return 53 because you doesn't give it the test case input... but replacing the num initialization with the correct value given in test case it return 53 as expected ^^
13th Jun 2021, 6:03 AM
visph
visph - avatar