Decimal to binary python algorithm

BigBoss

I'm trying to create an algorithm that converts decimals to binary. I can't use the built-in function in python to do so. This is what I've got.

n=int(input("enter a number"))

while n > 1:
    print(n%2)
    n //= 2
    if n % 2 ==0:
        print(n%2)
    else:
        print(n%2)

I'm completely fine with the 1's and 0's being printed in a separate line, as long as they're correct.

Dipen Dadhaniya

It should be:

n=int(input("enter a number\n"))

while n >= 1:    # Should be >= 1, not > 1.
    print(n%2)
    n //= 2
    # Removed if else.

Also, note that this will print binary in the reverse order.

For the input 6, the output will be:

0
1
1

Not:

1
1
0

If you want the later one, then you can store it in a list first and then print the list in the reverse order.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Algorithm to convert a 2d binary list to decimal numbers in Python

Decimal to binary with visualisation in python

Decimal to Binary function, Python

Binary to Decimal conversion in Python

Binary to Decimal Conversion in Haskell using Horners Algorithm

Binary search Algorithm - Python

Python Improve Binary algorithm

Python program for converting decimal to binary

Python - Converting Binary (list) to Decimal

binary to decimal using recursion,python

convert 32 bit, binary number to decimal in python

What are the necessary requirements for coding decimal to binary [Python]?

Simple function to convert decimal number to binary in Python

Numpy arrax of integers from decimal to binary in python

String Index Error for Binary to Decimal Python Solution

Converting Decimal Number To Binary In Python 3

Basic Binary to Decimal Conversion Program not working (Python)

Python 3.6 converting 8 bit binary to decimal

Converting decimal numbers to binary with functions in python

Convert 32 bit binary to a decimal in python

Binary To Decimal

Unexpected behavior of binary search algorithm in python

18-bit Signed Binary number to Decimal in Python

Decimal to binary Half-Precision IEEE 754 in Python

How can I convert bytes object to decimal or binary representation in python?

convert a binary number (consists of the sign, mantissa and exponent ) to decimal number in python

how to convert decimal to binary by using repeated division in python

How to convert decimal to binary value of rows in multindex dataframe (python)?

How can I convert decimal to binary values in python?