Given two binary numbers, write a Python program to compute their sum.
Input: a = "11", b = "1" Output: "100" Input: a = "1101", b = "100" Output: 10001
- Naive Approach: The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum becomes more than 1, then store carry for the next digits.
- Using inbuilt function: Calculate the result by using the inbuilt bin() and int() function.
Method 1: Naive Approach:
The idea is to start from the last characters of two strings and compute digit sum one by one. If the sum becomes more than 1, then store carry for the next digits.
Method 2: Using inbuilt functions:
We will first convert the binary string to a decimal using int() function in python. The int() function in Python and Python3 converts a number in the given base to decimal. Then we will add it and then again convert it into a binary number using bin() function.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course