Converting a Decimal Number Into Binary Along With Code

Converting a Decimal Number Into Binary Along With Code

To convert any decimal number into binary, it is divided by 2, the remainder is pushed to the stack and the quotient is again divided by 2. On every division, the remainder is pushed to the stack and the quotient is re-divided by 2. The process continues until the quotient becomes less than 2. When the quotient becomes less than 2, that quotient too is pushed to stack. All, the remainders that are in stack are popped out in LIFO order. The values popped from the stack makes the binary version of the decimal number.

For example, let us assume that we want to convert 13 into binary. Number 13 is divided by 2, quotient will be 6 and remainder will be 1. The remainder 1 is pushed to stack (it will be stored at top=0) i.e. at location stack[0] (see Figure 1). The quotient 6 is again divided by 2, the quotient will be 3 and remainder will be 0. The remainder 0 is pushed to stack (at top=1) i.e. at stack[1] location. The quotient 3 is divided by 2, the quotient will be 1 and remainder too will be 1. The remainder 1 is pushed to stack at top=2 location i.e. at stack[2] location. Because, the quotient 1 is less than 2, it can’t be divided by 2 and is pushed to stack at top=3 location i.e. at stack[3] location.

The values in stack are popped one by one i.e. 1101 is the value that is popped from stack and it is the binary version of decimal value 13.

Figure 1 - Decimal number divided by 2 and remainders pushed to stack

C Program to convert decimal number to binary number

#include <stdio.h>

#define max 10

/* You can increase the array size to a bigger number if you think user can enter a very large value */

int top=-1;

int stack[max];

main()

{

  int j, numb, remainder, temp;

  printf("Enter a number in decimal ");

  scanf("%d",&numb);

  /* If you want to enable the user to enter value bigger than 32,767

     (its a limit of an int type), use long int i.e. replace %d by %ld.

     With long integer, user can enter a value between -2,147,483,647 to

     2,147,483,647 */

  temp=numb;

  while(numb >0)

  {

     remainder=numb%2;

     push(remainder);

     numb=numb/2;

  }

  printf("The binary conversion of %d is ", temp);

  while(top >=0)

  {

     j=pop();

     printf("%d", j);

  }

}

push(int m)

{

  top++;

  stack[top]=m;

}

int pop()

{

  int j;

  if(top==-1)return(top);

  else

  {

    j=stack[top];

    top--;

    return(j);

  }

}

 

To confirm if the binary value 1101 is correct answer for decimal value 13, let us convert binary number, 1101  back into decimal. To convert any binary value to decimal, every digit is multiplied by 2 to the power of a superscript. The value of superscript begins with 0 for the right most digit and is incremented by 1 on moving left. Like, 1 (the right most binary digit is multiplied by 20, 0 i.e. the binary digit on left of 1 is multiplied by 21, the next digit to left i.e. is multiplied by 22 and the left most digit 1 is multiplied by 23.

The result of each multiplication is then added to get the decimal value. The entire procedure is explained in Figure 2.

Figure 2 - Converting binary number to decimal