Write a Program to Convert Decimal to Binary

Decimal To Binary
INTRODUCTION:
Converting a decimal number to binary is a little more complex. Once you get the hang of it though it's fairly straightforward. There are a few ways you may approach converting decimal to binary but the method I find most convenient is repeated division by 2.
With repeated division by 2, we repeatedly divide the number by 2 and record the remainder. Once you've done this, all the way down to 0, you read the remainder digits from bottom to top and you have your binary number. The example below will illustrate. The result of division by 2 is listed on the left and the remainder is listed on the right.
Suppose we are converting the decimal number (87)10. Now the conversion is shown below:


So, the binary equivalent of (14)10 = (1110)2 After the dash (-) remainder is written.



ALGORITHM:

Decimal_to_Binary (Decimal_number,
                               Quotient, Binary, J)

STEP 1: [INITIALIZE] SET I=1

STEP 2: [INITIALIZE] SET Quotient = Decimal_number

STEP 3: Repeat step 4 and 5 WHILE Quotient >=1

STEP 4: SET Binary[I+1] = Quotient % 2

STEP 5: SET Quotient = Quotient / 2

STEP 6: Repeat step 7 FOR J = I -1 TO 1

               SET J = J – 1
                                        [END OF LOOP]

STEP 7: PRINT “ Binary [J] ”

STEP 8: EXIT



FLOWCHART:




PROGRAMME:


VARIABLES:













OUTPUT CHECKING:




DISCUSSION:
Converting from decimal to binary notation is slightly more difficult conceptually, but can easily be done once you know how through the use of algorithms. Begin by thinking of a few examples. We can easily see that the number 3= 2+1. and that this is equivalent to (1*2^1)+(1*2^0). This translates into putting a "1" in the 2^1 column and a "1" in the 2^0 column, to get "11". Almost as intuitive is the number 5: it is obviously 4+1, which is the same as saying [(2*2) +1], or 2^2+1. This can also be written as [(1*2^2)+(1*2^0)]. Looking at this in columns,
    2^2 | 2^1 | 2^0
     1     0     1
or 101. What we're doing here is finding the largest power of two within the number (2^2=4 is the largest power of 2 in 5), subtracting that from the number (5-4=1), and finding the largest power of 2 in the remainder (2^0=1 is the largest power of 2 in 1). Then we just put this into columns. This process continues until we have a remainder of 0. Let's take a look at how it works. We know that:
   2^0=1
   2^1=2
   2^2=4
   2^3=8
   2^4=16
   2^5=32
   2^6=64
   2^7=128
and so on. To convert the decimal number 75 to binary, we would find the largest power of 2 less than 75, which is 64. Thus, we would put a 1 in the 2^6 column, and subtract 64 from 75, giving us 11. The largest power of 2 in 11 is 8, or 2^3. Put 1 in the 2^3 column, and 0 in 2^4 and 2^5. Subtract 8 from 11 to get 3. Put 1 in the 2^1 column, 0 in 2^2, and subtract 2 from 3. We're left with 1, which goes in 2^0, and we subtract one to get zero. Thus, our number is 1001011.

Post a Comment

0 Comments