Tech
Infected

Linux, OpenSource, Programming And Hacks

May 31, 2018

Python Program to Add two binary numbers using built function

May 31, 2018

Here we will see how to easily add two numbers in python3 using built-in functions.

please run this program with python3.

Steps:

1. Take input of the two binary numbers as string using the input function.
2. convert them into integers using int function passing the base 2
3. Add the integers
4. convert the sum into binary using the bin function

Same steps for subtracting, multiplication, or division, just change the operation in step 3

a = input("Enter 1st binary number: ")
b = input("Enter 2nd binary number: ")

#converting a and b into integer

a = int(a,2)
b = int(b,2)

#adding the integers

s = a + b

#sonverting the sum into binary

s = bin(s)

print(s)



No comments:

Post a Comment