Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help with this 1.2. Write a Python program which will accept a non-negative decimal integer from the user and produce the binary equivalent

i need help with this image text in transcribed
image text in transcribed
1.2. Write a Python program which will accept a non-negative decimal integer from the user and produce the binary equivalent using the algorithm from section 4.2.1, page 156 of our textbook. Program requirements: 1.1. Do not use recursion, Python lists, the list() function, the reversed() function, bin(), string indexing, the Python for loop, or the .split() method. Do not define a function to do the decimal-to-binary conversion. Do demonstrate that you can implement a step-by-step algorithm to accomplish the conversion. 1.3. Do not treat the user's input as a series or list or sequence of characters. 1.4. Include both header comments and explanatory comments in the code. 1.5. Include an explanatory comment in the code for each loop and for each "if", "elif" and "else". Display a short explanation of what the program does. 1.7. Prompt for, and reads, an integer from the user. Immediately convert the user input into a decimal integer using the int() function. 1.8. Reject any number less than zero. 1.9. Create a variable containing an empty character string. 1.10. Create a copy of the user's integer in an integer variable named "working". (This is so that we still have the original value available if we need it, for example in step 1.12 below.) 1.11. Use a loop and one or more conditional statements (if/elif/else) to process the integer using successive division by 2, as described in section 4.2.1, page 156 or our textbook. The loop will most likely do the following operations: 1.11.1. If the working number is odd, prepend the character string "1" to your string variable, otherwise prepend the character string "0" to your string variable. 1.11.2. Print the working number and the character string. 1.11.3. Divide the working number by 2, throwing away the remainder. (This throws away the bit you just tested.) 1.11.4. If the working number is zero, break out of the loop. If the working number is not zero, run through the loop again. 1.12. After the working number has been processed, report the user's original number and its binary representation as stored in the character string you have built Test your program with several positive values, then test it with zero. Fix as needed. Turn in your program to this week's assignment folder. 2. 3. pilluuli UI WIL vigindl Urmal Value. For example, here is the conversion of the decimal value 19 into binary: Convert the value 19 to binary: + 2 quotient = 9 remainder = 1 9= 2 quotient = 4 remainder = 1 order for quotient = 2 remainder - 0 reading the 2+2 quotient = 1 remainder - 0 remainder 1+2 quotient -o remainder = 1 digits Stop, because the quotient is now 0. 4+2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions