Question

Python question: Write a program that takes in a positive integer as input, and outputs a...

Python question:

Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:

As long as x is greater than 0 Output x modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#Reading a decimal number input from user
num = eval(input("Enter value: "))
#Result is used to store binary value
result = ""
#Repeat the loop till n becomes negative
while (num > 0):
    #Finding the reminder
    rem = num % 2
    #Appending reminder to the result
    result += str(rem)
    #Making num to its half
    num = num // 2
#Reverse the value of result
result = result[::-1]
#Printing final binary value to screen
print(result)

Output

Enter value: 12 1100 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Python question: Write a program that takes in a positive integer as input, and outputs a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT