Converting Decimal Number To Binary Number In Python
In mathematics and computer science, binary number are those number having the base of 2. In other words, they are written interms of 0 and 1 or machine code that only computer can understand in order to execute certain tasks. The following example is a code in python that asks user to enter the number in order to find its binary equivalent and also prints the result. The logic is very simple inorder to calculate the same. In python, you have in-built function to perform certain task within a second but I always prefer learning through my own way as it helps to sharpen your problem solving skills and also increases your understanding of various concepts.
a = int(input("Enter a decimal number: "))
print("The binary value of {} is: ".format(a), end="")
c = ''
while a != 0:
remainder = a % 2
b = str(remainder)
c = c + b
a = int(a / 2)
d = int(c[::-1])
print(d)
We asked for input from user and then used a while loop to find the remainder. We created an empty string c at the beginning of the program. Inside while loop unless the condition becomes false, the empty string concatenates with the remainder. And we need to run the loop until the dividend becomes 0. In python, the division is pretty straightforward. You can divide any number without specifying whether it is integer value, float value, double value and so on. So I used int() function to convert the dividend to integer. After coming out of while loop, we reverse the string in order to get our desired output using the concept of slice.
Lets try executing this code by converting 27 into its equivlent binary number which is 11011. You can see the same output:
In mathematics and computer science, binary number are those number having the base of 2. In other words, they are written interms of 0 and 1 or machine code that only computer can understand in order to execute certain tasks. The following example is a code in python that asks user to enter the number in order to find its binary equivalent and also prints the result. The logic is very simple inorder to calculate the same. In python, you have in-built function to perform certain task within a second but I always prefer learning through my own way as it helps to sharpen your problem solving skills and also increases your understanding of various concepts.
a = int(input("Enter a decimal number: "))
print("The binary value of {} is: ".format(a), end="")
c = ''
while a != 0:
remainder = a % 2
b = str(remainder)
c = c + b
a = int(a / 2)
d = int(c[::-1])
print(d)
We asked for input from user and then used a while loop to find the remainder. We created an empty string c at the beginning of the program. Inside while loop unless the condition becomes false, the empty string concatenates with the remainder. And we need to run the loop until the dividend becomes 0. In python, the division is pretty straightforward. You can divide any number without specifying whether it is integer value, float value, double value and so on. So I used int() function to convert the dividend to integer. After coming out of while loop, we reverse the string in order to get our desired output using the concept of slice.
Lets try executing this code by converting 27 into its equivlent binary number which is 11011. You can see the same output:
Leave Converting Decimal Number To Binary Number In Python to:
Read more #stem posts
Best Posts From Leo Umesh
We have not curated any of leoumesh's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Leo Umesh
- The Tattoo of My Newborn Baby Girl Forever Inked on My Skin
- Long Weekend Food Feast With Wife
- String Manipulation in C#: Part 2
- String Manipulation in C#
- Introduction to C# and .NET
- Weekend Escape To Falls
- Evening Barbecue With Favourite Beer 🍺 🍻
- NumPy Broadcasting
- Summer BBQ
- Review of "The Day of The Jackal" Series
- Wonders of Wave Rock, Western Australia
- Trying To Make Authentic Chicken Dum Biryani At Home
- Beautiful Ocean Lookout in South West Australia
- Esperance: Hidden Paradise of Western Australia
- Cruising Through Blue Haven Beach
- My First Tattoo
- Exploring Flavoursome Veggie Indian Cuisines
- Australia's Popular Drink: Homemade
- An Everlasting Flowers
- Unexpected Rainbow At Wellington Dam, Western Australia