filmov
tv
Decimal to Binary in Python || Lesson 26.1 || Python || Learning Monkey ||

Показать описание
#python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
Decimal to Binary in Python
In this class, we write a program to convert decimal to binary in python.
Decimal to Binary
Q) Write a program to convert the given decimal number to binary.
The programmer should have some basic knowledge of loops. Click here.
Try to write the program on your own and check for a solution. For best practice.
Logic
Example:
Decimal number 72
72 divide by 2. quotient =36 reminder =0
36 divide by 2. quotient =18 reminder =0
18 divide by 2. quotient =9 reminder =0
9 divide by 2. quotient= 4 reminder =1
4 divide by 2. quotient =2 reminder= 0
2 divide by 2. quotient =1 reminder =0
1 divide by 2. quotient =0 reminder =1
Logic is to divide the given number by two until we get zero.
Each time reminder value is considered.
Write the reminders from last to first. We will get 1001000, which is the binary value of decimal 72.
Output is displayed in integer format.
In order to convert the binary numbers to integer value. the below logic is used.
Take the binary values from left to right one by one. ie reminder values.
Initially take variable binary as zero. Do the following to convert to integer.
binary = rem * position + binary
binary = 0 * 1 + 0 = 0
binary = 0 * 10 + 0 = 0
binary = 0 * 100 + 0 = 0
binary = 1 * 1000 + 0 = 1000
binary = 0 * 10000 + 1000 = 1000
binary = 0 * 100000 + 1000 = 1000
binary = 1 * 1000000 + 1000 = 1001000
Finally, binary = 1001000. The final binary value is equal to our binary number.
This logic will help you a lot in solving the complex programs in our later classes.
Program
number=int(input("enter your number"))
x=number
rem=0
place=1
binary=0
while(x0):
rem=x%2
binary=(rem*place)+binary
x//=2
place*=10
print(number,"\t",binary)
In the above program while loop executing till number is zero.
Each time reminder value is taken using modulo operator and applying the above-discussed logic to convert to integer.
Link for playlists:
Decimal to Binary in Python
In this class, we write a program to convert decimal to binary in python.
Decimal to Binary
Q) Write a program to convert the given decimal number to binary.
The programmer should have some basic knowledge of loops. Click here.
Try to write the program on your own and check for a solution. For best practice.
Logic
Example:
Decimal number 72
72 divide by 2. quotient =36 reminder =0
36 divide by 2. quotient =18 reminder =0
18 divide by 2. quotient =9 reminder =0
9 divide by 2. quotient= 4 reminder =1
4 divide by 2. quotient =2 reminder= 0
2 divide by 2. quotient =1 reminder =0
1 divide by 2. quotient =0 reminder =1
Logic is to divide the given number by two until we get zero.
Each time reminder value is considered.
Write the reminders from last to first. We will get 1001000, which is the binary value of decimal 72.
Output is displayed in integer format.
In order to convert the binary numbers to integer value. the below logic is used.
Take the binary values from left to right one by one. ie reminder values.
Initially take variable binary as zero. Do the following to convert to integer.
binary = rem * position + binary
binary = 0 * 1 + 0 = 0
binary = 0 * 10 + 0 = 0
binary = 0 * 100 + 0 = 0
binary = 1 * 1000 + 0 = 1000
binary = 0 * 10000 + 1000 = 1000
binary = 0 * 100000 + 1000 = 1000
binary = 1 * 1000000 + 1000 = 1001000
Finally, binary = 1001000. The final binary value is equal to our binary number.
This logic will help you a lot in solving the complex programs in our later classes.
Program
number=int(input("enter your number"))
x=number
rem=0
place=1
binary=0
while(x0):
rem=x%2
binary=(rem*place)+binary
x//=2
place*=10
print(number,"\t",binary)
In the above program while loop executing till number is zero.
Each time reminder value is taken using modulo operator and applying the above-discussed logic to convert to integer.
Link for playlists:
Комментарии