Binary Representation Generator

Write a Python program that asks for an integer n and then prints out the binary representation of that number.

Example 1:

Input: n = 5 

Output: 101

Example 2:
Input: n = 8 

Output: 1000

Use the divide by 2 method to find the binary representation of a number.

n = int(input("Enter a number: "))
binary = ''
while n > 0:
    binary = str(n % 2) + binary
    n = n // 2
print(binary)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!