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.

Answer from beauxq on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-decimal-numbers-to-binary-by-a-while-loop-in-Python
How to convert decimal numbers to binary by a while loop in Python - Quora
Answer (1 of 8): 1. To convert decimal numbers to binary using a while loop in Python, you can use the following code: [code]def decimal_to_binary(num): binary_num = "" while num > 0: binary_num = str(num % 2) + binary_num num = num // 2 return binary_num [/code]
๐ŸŒ
Know Program
knowprogram.com โ€บ home โ€บ decimal to binary in python
Decimal to Binary in Python - Know Program
April 2, 2021 - # Python program to convert decimal to binary using while loop import math #importing math-module # take input num = int(input('Enter any decimal number: ')) rem='' while num>=1: rem+=str(num%2) num=math.floor(num/2) # convert binary bin="" ...
Top answer
1 of 2
3

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.

2 of 2
0
B_Number = 0
cnt = 0
while (N != 0):
    rem = N % 2
    c = pow(10, cnt) 
    B_Number += rem * c 
    N //= 2
    
    # Count used to store exponent value 
    cnt += 1

return B_Number 
๐ŸŒ
CodeScracker
codescracker.com โ€บ python โ€บ program โ€บ python-program-convert-decimal-to-binary.htm
Python Program to Convert Decimal to Binary
Before condition evaluates to be false, here are the list of values one by one after each evaluation of the loop: ... Now print the value of bnum[] list from its last to 0th index. Therefore, the binary equivalent of given decimal number (50) is 110010 ยท This is the modified version of previous program. The end= is used to skip printing of an automatic newline using print(). The str() method is used to convert any type of value to a string type. print("Enter Decimal Number: ", end="") d = int(input()) i = 0 b = [] while d!=0: b.insert(i, d % 2) i = i+1 d = int(d / 2) i = i-1 print(end="\nEquivalent Binary Value = ") while i>=0: print(end=str(b[i])) i = i-1 print()
๐ŸŒ
VR Soft Tech
vrsofttech.com โ€บ python-programs โ€บ python-program-convert-decimal-to-binary
Python Program to Convert Decimal to Binary using While Loop. - vrsofttech
The function binary converts a decimal (base 10) integer to its binary (base 2) representation.This line defines a function named binary that takes one parameter, decimal. Inside the loop, the expression decimal % 2 computes the remainder when decimal is divided by 2.
๐ŸŒ
Similar Geeks
similargeeks.com โ€บ home โ€บ python program to convert decimal to binary using while loop
Python Program to Convert Decimal to Binary using While loop
January 4, 2022 - The below program converts a given decimal value into a binary representation using a while loop. ... # Python program to convert decimal to binary using while loop #importing math module import math # input num = int(input('Enter any decimal number: ')) rem='' while num>=1: rem+=str(num%2) num=math.floor(num/2) # convert binary bin="" for i in range(len(rem)-1,-1,-1): bin = bin + rem[i] # display result print('Binary value:', bin)
๐ŸŒ
BTech Geeks
python-programs.com โ€บ home โ€บ python program to convert binary to decimal using while loop
Python Program to Convert Binary to Decimal using While Loop - Python Programs
November 22, 2021 - In the previous article, we have discussed Python Program to Convert a Decimal Number to Binary Given a binary number and the task is to get the respective decimal number using the while loop. Examples: Example1: Input: Given Binary Number = 1000 Output: The Decimal value of the given binary number { 1000 } is: [โ€ฆ]
๐ŸŒ
BTech Geeks
btechgeeks.com โ€บ home โ€บ convert binary to int python โ€“ python program to convert binary to decimal using while loop
Convert binary to int python - Python Program to Convert Binary to Decimal using While Loop - BTech Geeks
September 17, 2024 - Convert binary to int python: In the previous article, we have discussed Python Program to Convert a Decimal Number to Binary Given a binary number and the task is to get the respective decimal number using the while loop.
Find elsewhere
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python program to convert decimal to binary, octal and hexadecimal
Python Program To Convert Decimal To Binary, Octal And Hexadecimal
February 4, 2025 - Take a decimal number as input ... r, which is used to store the current remainder, as 0. Then, apply a while loop with the condition that n does not equal 0. This loop will run till n is not equal to 0....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - Python
n = 7 # decimal number res = '' # binary result while n > 0: res = str(n & 1) + res n >>= 1 print(res) ... Explanation: while loop extracts the least significant bit of n using n & 1, prepends it to res and right-shifts n (n >>= 1) to process ...
Published: July 11, 2025
๐ŸŒ
Easycodebook
easycodebook.com โ€บ home โ€บ perfect programming tutorials โ€“ blog โ€บ python decimal to binary converter
Python Decimal to Binary Converter - EasyCodeBook.com
July 21, 2019 - # This Python script / program ... (c) num = int(input('Enter a Decimal number to Convert into Binary:')) q=num binary=0 i=0 while q!=0: rem = q%2 binary = binary + rem * (10**i) q = q // 2 i+=1 print('Binary Number=',binary) The output ...
๐ŸŒ
Allinpython
allinpython.com โ€บ home โ€บ how to convert decimal to binary in python
How to Convert Decimal to Binary in Python โ€“ allinpython.com
August 15, 2023 - From the above algorithm and explanation, we now know how to write a program in Python to convert a decimal number to a binary number. However, before writing the program, there are a few programming concepts you need to be familiar with. ... n = int(input("Enter a number:")) # Create a Empty list to store binary numbers binary_list = [] # store remainders using while loop while n!=0: rem = n%2 binary_list.append(rem) n = n//2 # Reverse a list binary_list = binary_list[::-1] # print binary numbers print("Binary of given number is ",end='') for num in binary_list: print(num, end='')
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ python program โ€บ python program for decimal to binary conversion
Decimal to binary conversion in python | PrepInsta
September 29, 2022 - def convertBinary(num): binary = 0 i, rem = 1, 0 while num != 0: rem = num % 2 num = num // 2 binary += rem * i i *= 10 return binary decimal_num = 21 print("Binary of", decimal_num, "is : ", end="") print(convertBinary(decimal_num))
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-program-to-covert-decimal-to-binary-number
Convert Decimal to Binary Number - GeeksforGeeks
n = 7 # decimal number res = '' # binary result while n > 0: res = str(n & 1) + res n >>= 1 print(res) ... Explanation: while loop extracts the least significant bit of n using n & 1, prepends it to res and right-shifts n (n >>= 1) to process ...
Published: March 19, 2025
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ decimal-binary-recursion
Python Program to Convert Decimal to Binary Using Recursion
To understand this example, you should have the knowledge of the following Python programming topics: ... Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ programs and examples โ€บ python convert decimal number to binary and vice versa
Python Convert Decimal Number to Binary and Vice Versa โ€“ PYnative
April 22, 2025 - decimal = 20 binary = "" if decimal == 0: binary = "0" else: while decimal > 0: remainder = decimal % 2 # Get the remainder which is the last binary digit (0 or 1) binary = str(remainder) + binary # Add it to the front of the result decimal = decimal // 2 # Divide number by 2 (integer division) ...
๐ŸŒ
CodeScracker
codescracker.com โ€บ python โ€บ program โ€บ python-program-convert-binary-to-decimal.htm
Python Program to Convert Binary to Decimal
Python Program to Convert Binary to Decimal - In this article, we've created some programs in Python, to convert binary number entered by user at run-time to its equivalent decimal value. Binary to Decimal using while loop, Using for loop, int(), function, class