Python Program to Converter a Decimal to Binary
You are Here:
Converter a Number from Decimal to Binary
In the following example, we will convert a Decimal Number (500) to Binary Number (111110100).
Tips: It is recommended to use our online Decimal to Binary calculator for better understanding.
Example
Python Compiler
import math
num = 500
arr = [0]*50
i = -1
while(num != 0):
i += 1
arr[i] = num % 2
num = math.floor(num / 2)
print("Binary number of 500 (decimal) is ", end="")
for j in range(i, -1, -1):
print(arr[j], end="")
Output
Binary number of 500 (decimal) is 111110100
Converter any Given Decimal Number to Binary Number
In the following example, we will convert any given decimal number to a binary number.
Example
Python Compiler
import math
num = int(input("Enter a Decimal Number: "));
copyNum = num;
arr = [0]*50
i = -1
while(copyNum != 0):
i += 1
arr[i] = copyNum % 2
copyNum = math.floor(copyNum / 2)
print("Binary number of %d (decimal) is " % num, end="")
for j in range(i, -1, -1):
print(arr[j], end="")
Output
Enter a Decimal Number: 10
Binary number of 10 (decimal) is 1010
Reminder
Hi Developers, we almost covered 90% of String functions and Interview Question on Python with examples for quick and easy learning.
We are working to cover every Single Concept in Python.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.