Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

python 3.6 def binary(n): Given a n int n, convert it to binary. Hint: each time

ID: 3858556 • Letter: P

Question

python 3.6

def binary(n):

Given a n int n, convert it to binary. Hint: each time you divide a number by two, the remainder of 1 or 0 is the rightmost bit in its binary representation; continually dividing by 2 , the remainders will give you more and more bits in its binary representation from right to left , until the

division itself (not the remainder) reaches zero and you're done.

Example: n is eleven. 11%2==1 , and 11//2==5. 5%2==1, and 5//2==2. 2%2==0. and 2//2==1. 1%2==1, and 1//2==0 (quotient is zero, so

we're done). In reverse, that gave us 1011(10112==2**3 + 2**1 + 2**0 == 8+3+1 == 1110).

•Assume: n is a non-negative int

.•Restriction: do not call int()

.•Examples:binary(0)"0"

binary(5)"101"

binary(12)"1100"

binary(1030)"1000000110"

Explanation / Answer

def binary(n):
#Function to print binary number
if n > 1:
binary(n//2)
print(n % 2,end = '') #prints the binary value

# Take decimal number from user
number= int(input("Enter an integer: "))
binary(number) #calling the binary function

Ouput:

Enter an integer: 5

101