NO PYTHON 8 PLEASE.! it\'s PEP/8 the assembler nothing to do with python. Write
ID: 3747529 • Letter: N
Question
NO PYTHON 8 PLEASE.! it's PEP/8 the assembler nothing to do with python.
Write a program in Pep/8 or Pep/9 object code that will read an integer between 0 and 15 from the keyboard and display its equivalent in binary.
Example:
Input: 11
Output: 1011
Hint: you must take the remainder of the division of the integer by 2 and divide and again take the remainder of new quotient for a total of 4 times, each time saving the remainder and then display the four remainders in reverse order. For example, remainder of 11 over 2 is 1, remainder of 5 over 2 is 1, remainder of 2 over 2 is 0 and remainder of 1 over 2 is 1. Printing these remainders in reverse order produces 1011.
Explanation / Answer
#!/usr/bin/python
def decimaltobinary():
binary = ""
num = input("Enter a number between 0 to 15 : ")
if num > 15 or num < 0:
print ("Invalid number")
else:
while num > 0:
if num % 2 == 0:
binary = binary + "0"
else:
binary = binary + "1"
num = int(num / 2)
print("Binary of the given number:", binary[::-1])
if _name_ == '__main__':
decimaltobinary()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.