Program to add two Binary numbers using a function

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
  1. Create a function binary.
  2. Use the def keyword to define the function.
  3. Pass two parameters i.e. binary numbers.
  4. Covert the binary numbers to the integers and then add them.
  5. Convert the addition to the binary number using bin().
  6. To remove the starting 0b from the number using indexing.
  7. Print the output.
  8. 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

create a library management system.

search words in a string.

Leave a Comment