Python Challenges: Add two binary numbers
Write a Python program to add two binary numbers.
Explanation:
Sample Solution:
Python Code:
def add_binary_nums(x,y):
max_len = max(len(x), len(y))
x = x.zfill(max_len)
y = y.zfill(max_len)
result = ''
carry = 0
for i in range(max_len-1, -1, -1):
r = carry
r += 1 if x[i] == '1' else 0
r += 1 if y[i] == '1' else 0
result = ('1' if r % 2 == 1 else '0') + result
carry = 0 if r < 2 else 1
if carry !=0 : result = '1' + result
return result.zfill(max_len)
print(add_binary_nums('11', '1'))
print(add_binary_nums('10', '10'))
print(add_binary_nums('111', '111'))
print(add_binary_nums('1111111', '1'))
Sample Output:
100 100 1110 10000000
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to find the length of the last word.
Next: Write a Python program to find the single number which occurs odd numbers and other numbers occur even number.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics