Learn about Python Program to Convert Decimal to Binary using While loop in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Python Program to Convert Decimal to Binary using While loop
The below program converts a given decimal value into a binary representation using a while loop.
Program:
# 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)
Output:
Enter any decimal number: 55
Binary value: 110111
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Decimal to Binary in Python using recursion