Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python

How can I build a program to add two binary numbers without built-in functions?!

28th Dec 2023, 4:19 PM
Free palestine
Free palestine - avatar
3 Answers
+ 1
Free palestine , # If you want to accept binary input, the # int() function takes a second argument # for the base (default is 10). n = int(input(), 2) # base 2, binary #That way, the user can enter binary # numbers such as 10110000. Then # do whatever math to the variable(s). n *= n # To print the answer in binary, insert # it into an f-string with a # binary format specifier (:b). print(f"Answer: {n:b}")
30th Dec 2023, 2:35 PM
Rain
Rain - avatar
+ 6
write the numbers in binary format, e.g. 0b10 then add the numbers
28th Dec 2023, 5:48 PM
Lisa
Lisa - avatar
+ 4
Hi, Free palestine ! To devellop a program for adding binary numbers without using built in functions, here is a simplified approach: 1 ) Understand binary addition (First, practice adding binary numbers manually withh paper and pen. Pay special attention to how the carry digit works in each step.) 2) Create a pseudocode (Once you're comfortable with the manual procsess, draft a pseudocode. This will help you understand the logic and structure of your program before you start coding. 3) Implement in Python (Convert your pseudocode into Python code. Working with ’strings can be easier for this task’, as it allows you to manipulate the binary digits directly witho ut any need to convert them.)
28th Dec 2023, 6:53 PM
Per Bratthammar
Per Bratthammar - avatar