Question: Given the code below: # Main Program n1=int( input (Enter n1: )) n2=int( input (Enter n2: )) ans =n1+n2 str1 = decimalToBinary (n1) print(The binary

 Given the code below: \# Main Program n1=int( input ("Enter n1:")) n2=int( input ("Enter n2: ")) ans =n1+n2 str1 = decimalToBinary (n1)print("The binary representation of", n1, "is", str1) str2 = decimalToBinary (n2) print("The

Given the code below: \# Main Program n1=int( input ("Enter n1: ")) n2=int( input ("Enter n2: ")) ans =n1+n2 str1 = decimalToBinary (n1) print("The binary representation of", n1, "is", str1) str2 = decimalToBinary (n2) print("The binary representation of", n2, "is", str2) str3 = binaryAddition (str1, str2) print( "The binary addition of", n1, "and", n2, "is", str 3 ) n3 = binaryToDecimal (str3) print("Converting the binary to decimal gives", n3) if ans ==n3 : else: print("Since", ans, "==", n3, ", it seems we did good job.") print("Since", ans, " !=",n3,", something went wrong.") define the three functions decimalToBinary, binaryAddition, and binaryToDecimal so that the program will be able to add binary numbers. The description of each function is as follows: def decimalToBinary (n) : - This function takes a positive integer n>0 as an argument and returns a string of integers where each character of the string is a binary digit, 0 or 1 obtained by the conversion of the integer argument n to binary. You are not allowed to use the built-in function bin(). For example, decimalToBinary (3) "11" decima1ToBinary (123) "111011" decimalToBinary (2312) "100100001000" binaryAddition(str1, str2): - This function takes two string arguments str1 and str 2 representing two binary numbers and returns a string of integers whose characters are obtained by the addition of the two binary numbers in binary addition format. - How to add two binary numbers? Binary addition is like decimal addition, except that it carries on a value of 2 instead of a value of 10. 0+0=01+0=10+1=11+1=10(thisis0carry1)1+1+1=11(thisis1carry1) To add two binary numbers, we simply add together the corresponding digits in each binary number from right to left and if the sum is greater than what fits in a single digit, we carry a 1 into the next column. For example, 11111carries110101+111011010010 We start by the first column from the right: 1) First column: 1+1=0 (with carry 1) 2) Second column: ++1 (carried) =1 (no carry) 3) Third column: 1+1+ no carry =0 (carry 1) 4) Fourth column: 0+1+1 (carried) =0 (carry 1 ) 5) Fifth column: 1+1+1 (carried) =1 (carry 1 ) 6) Sixth column: 1+1 (carried) =0 (carry 1 ) 7) Seventh column: 1 (carried) binaryAddition("110101", "11101") "1010010" binaryAddition("110101", "11101") "1010010" def binaryToDecimal(binstr): - This function takes a string binstr representing a binary number and converts it to decimal and returns the decimal number. For example, binaryToDecimal("10") 2 binaryTodecimal("10100") 20 binaryToDecimal("101110110010") 2994 binaryToDecima1("1111101110110000") 64432

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!