Saturday 1 February 2014

Program to Convert Binary Number to Decimal.

Hey Guys! Ever got frustrated doing manual calculations for converting BINARY NUMBERS to DECIMAL? If Yes! You may get relief from that now! Today i am gonna show you to write the program in Python which asks the user to enter the binary number and converts it to its decimal equivalent.

Before we get started we need to be very clear about how actually the binary number is converted to its decimal equivalent. So here it is:

  • Each number system has a base also called a Radix. A decimal number system is a system of base 10; binary is a system of base 2.What are these varying bases? The answer lies in what happens when we count up to the maximum number that the numbering system allows. In base 10, we can count from 0 to 9, that is,10 digits and in binary we count 0 and 1 i.e. two digits
  • Now method to convert Binary number to decimal:
    • Start at the rightmost bit.
    • Take that bit and multiply by 2^n where n is the current position beginning at 0 and increasing by 1 each time. This represents a power of two.
    • Sum each terms of product until all bits have been used.
  • Let us take an example:
    • Convert the Binary number 101011 to its Decimal equivalent
    • 1 * 2^5 + 0 * 2^4 + 1 * 2^3 + 0 * 2^2 + 1 * 2^1 + 1 * 2^0
    • 32 + 0 + 8 + 0 +2 + 1 = 43
So there we go, now we know how to convert binary to decimal. So let's have a look on the program.
Explanation source: Python Class XI CBSE book



This is the program which prompts the user to enter the binary number and converts it into Decimal number.
The program has been commented line by line for user's consent, Still if there is any query, the reader is free to express his/her views in the comments.

THE FOLLOWING IS THE OUTPUT OF THE PROGRAM:


Hope You Must have enjoyed the Post! Cheers! Enjoy! And Don't Forget to give your FeedBack!