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

hameed

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

i know i have to use a while loop, and use modulus sign and others {%} and {//} to do this...but i need some kind of example for me to understand how its done so i can understand completely. CORRECT ME, if I'm wrong:

number = int(input("Enter a numberto convert into binary: "))

result = "" 
while number != 0:
    remainder = number % 2 # gives the exact remainder
    times = number // 2
    result = str(remainder) + result
    print("The binary representation is", result)
    break

Thank You

beauxq

Making a "break" without any condition, makes the loop useless, so the code only executes once no matter what.

-

If you don't need to keep the original number, you can change "number" as you go.

If you do need to keep the original number, you can make a different variable like "times".

You seem to have mixed these two scenarios together.

-

If you want to print all the steps, the print will be inside the loop so it prints multiple times.

If you only want to print the final result, then the print goes outside the loop.

while number != 0:
    remainder = number % 2  # gives the exact remainder
    number = number // 2
    result = str(remainder) + result
print("The binary representation is", result)

-

The concatenation line:

Putting the print inside the loop might help you see how it works.

we can make an example:

the value in result might be "11010" (a string, with quotes)

the value in remainder might be 0 (an integer, no quotes)

str(remainder) turns the remainder into a string = "0" instead of 0

So when we look at the assignment statement:

result = str(remainder) + result

The right side of the assignment operator = is evaulated first.

The right side of the = is

str(remainder) + result

which, as we went over above has the values:

"0" + "11010"

This is string concatenation. It just puts one string on the end of the other one. The result is:

"0     11010"

"011010"

That is the value evaluated on the right side of the assignment statement.

result = "011010"

Now that is the value of result.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to convert a decimal number into a binary number using STACK in python

How to convert binary into decimal using <bitset> library?

How to convert binary to decimal using a for loop?

How to convert a decimal number to a binary number using recursive functions?

How to convert a decimal to binary in C programming using stack

How to convert large binary to decimal?

How to convert binary fraction to decimal

how to convert binary string to decimal?

How to convert a binary to decimal in Swift?

How can I convert bytes object to decimal or binary representation 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?

I can not convert the currency conversion, using Forex, to the integer for removing the decimal division, in Python

Converting Binary to Decimal Numbers in Java strictly using multiplication and division

binary to decimal using recursion,python

How to solve binary to decimal with fraction in c#? Using winform, Im trying to convert binary numbers into decimal. I used the input function

convert 32 bit, binary number to decimal in python

Simple function to convert decimal number to binary in Python

Convert 32 bit binary to a decimal in python

how to convert decimal value to binary bits?

How to convert binary string value to decimal

How to convert a decimal number to binary in Swift?

How to convert binary value to decimal in hive/impala

How to convert binary floating points to decimal fractions?

How to Convert Decimal to Binary in Assembly Language

Kotlin: How to convert from binary to decimal

Decimal fraction conversion to binary - how to convert 0.1?

how to convert csv list of decimal to binary

convert hex decimal numbers to decimal using python