Write an interactive python program to read an integer. If it is positive then display the corresponding binary representation of that number. The user must enter 999 to stop. In case the user enters a negative number, then ignore that input and ask the user to re-enter any different number
Answers
Answer:
different" (and any subsequent words) was ignored because we limit queries to 32 words.
Answer:
The following Python program will read an integer. If the number is positive then it will display the corresponding binary representation of that number. The user has to enter 999 to stop. In case the user enters a negative number, then the program will ask the user to re-enter any different number.
def intpartbinary(m):
a = []
n = int(m)
while n != 0:
a.append(n % 2)
n = n//2
a.reverse()
return a
# function to convert fractional part to binary number
def decimalpartbinary(m):
a = []
n = m-int(m)
while n != 0:
x = 2 * n
y = int(x)
a.append(y)
n = x-y
return a
def binarycode(m):
a = intpartbinary(m)
b = decimalpartbinary(m)
c =[]
for i in range(0, len(a)):
c.append(a[i])
c.append('.')
for j in range(0, len(b)):
c.append(b[j])
print('Binary code of given function is\n')
for k in range(0, len(c)):
print(c[k], end =' ')
# Driver Code
num = float(input("Enter a number: "))
while num <= 0:
num = float(input("Enter a number: "))
if num == 999:
sys.exit()
if num > 0:
print("Positive number")
binarycode(num)
Explanation:
Enter a number: 115
Positive number
Binary code of given function is
1 1 1 0 0 1 1 .
For same kind of problems, click here ->
https://brainly.in/question/30661691
https://brainly.in/question/31110476