Python Examples

Python Program to Convert Decimal to Binary Using Recursion

If the user wants to convert a decimal to binary number, we give a decimal number as the input, the compiler converts the decimal number into its equivalent binary number as the output. 

decimal to binary

Source

Decimal to Binary Converter

Source Code:

                    

def convertToBin(a):

   if a > 1:

       convertToBin(a//2)

   print(a % 2,end = '')


# decimal number

d = 34
convertToBin(d)

print()


Output

100010

The program given above is applicable for whole numbers. Decimal numbers cannot be converted using the logic given above. 

How do I convert decimal to binary?

To convert decimal to binary we divide the decimal number by two until the number is greater than zero, and write the remainders in the reversed order. 

For example:

Let us consider 17 as the decimal number. 

Step 1: When 17 is divided by 2, the remainder is one. Therefore, arr[0] = 1. 

Step 2: Now we divide 17 by 2. New number is 17/2 = 8. 

Step 3: When 8 is divided by 2, the remainder is zero. Therefore, arr[1] = 0. 

Step 4: Divide 8 by 2. New number is 8/2 = 4. 

Step 5: When 4 is divided by 2, the remainder is zero. Therefore, arr[2] = 0. 

Step 6: Divide 4 by 2. New number is 4/2 = 2. 

Step 7 : Remainder when 2 is divided by 2 is zero. Therefore, arr[3] = 0. 

Step 8: Divide 2 by 2. New number is 2/2 = 1. 

Step 9: When 1 is divided by 2, the remainder is 1. Therefore, arr[4] = 1. 

Step 10: Divide 1 by 2. New number is 1/2 = 0. 

Step 11: Since number becomes = 0. Now we consider the remainders in the reversed order. Therefore the equivalent binary number is 10001.

What does 10101 mean in binary?

10101 is a binary number whose decimal representation is 21.

What is the decimal 254 in binary?

To derive this, we divide 254 by 2 and use the quotient in this step as the dividend for the next. This process must be repeated till it becomes zero. We then write the remainder in reverse order.  Therefore, by following these steps we will achieve 11111110 as the binary equivalent.

What is 10101 as a decimal?

To find out what 10101 is as a decimal, one must note that a binary number has only two numbers i.e. zero and one. To convert 10101 into decimal, follow the steps given below:

Step 1: We start with the last digit of the number. Multiply 1 with 2^0, Multiply the 0 to the last digit by 2^1, Multiply the 1 to the last digit by 2^2, Multiply the 0 to the last digit by 2^3, Multiply the 1 to the last digit 2^4.

Step 2: Add the individual products together, and you be getting the sum of all the products. 

1 x 2^0 = 1

0 x 2^1 = 0

1 x 2^2 = 4

0 x 2^3 = 0

1 x 2^4 = 16

1 + 0 + 4 + 0 + 16 = 21

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.