In this python function program, we will add two Binary numbers using a function.
Binary Number: A number system where a number is represented by using only two digits (0 and 1) with a base 2 is called a binary number system.
Steps to solve the program
- Create a function binary.
- Use the def keyword to define the function.
- Pass two parameters i.e. binary numbers.
- Covert the binary numbers to the integers and then add them.
- Convert the addition to the binary number using bin().
- To remove the starting 0b from the number using indexing.
- Print the output.
- Pass the binary numbers to the function while calling the function.
def binary(n1,n2):
result = bin(int(n1,2)+int(n2,2))
print(f"Addition of binary numbers {n1},{n2}: ",result[2:]) #to get rid of 0b
binary('100010','101001')
Output :
Addition of binary numbers 100010,101001: 1001011
Related Articles
Python function program to search words in a string.
Python function program to get the length of the last word in a string.
Python function program to get a valid mobile number.
Python function program to convert an integer to its word format.
Python function program to get all permutations from a string.
Python function program to add two numbers.