LANGUAGE IS PYTHON
9.8 LAB: Convert to binary

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
Note: The above algorithm outputs the 0's and 1's in reverse order.

Ex: If the input is:

6
the output is:

011
6 in binary is 110; the algorithm outputs the bits in reverse.

Respuesta :

The program illustrates the use of modulo operators

The modulo operator is used to return the remainder of a division.

The program in Python, where comments are used to explain each line is as follows:

#This imports the math module

import math

integer_value = int(input())

#This initializes the remainder to an empty string

remainder = ""

#This loop is repeated while the integer input is greater than or equal to 1

while integer_value>=1:

   #This calculates the remainder after the integer value is divided by 2

   remainder+=str(integer_value % 2)

   #This gets the floor division of the integer input by 2

   integer_value=math.floor(integer_value/2)

#This reverses the string

binary_equiv = remainder[::-1]

#This prints the binary equivalent

print(binary_equiv)

At the end of the program, the binary string is printed

Read more about similar programs at:

https://brainly.com/question/24742544