# Convert Binary Number To Decimal Number Along With Source Code

Converting Binary Number To Decimal Number

To convert any binary number to decimal, there are two steps:

1. First step is to separate all binary digits of the binary number and store the individual bits in a queue. To separate binary digits, the binary number is divided by 10. On every division, the remainder is added to the queue and the quotient is divided by 10 again. The procedure continues until the quotient becomes less than 10. The last quotient too is added to the queue.
2. Second step is to take out digits from the queue one by one and multiply them with powers of 2.

Here is the first step. Let us assume, we want to convert a binary number 1101 into decimal. To separate each bit of the binary number, we need to divide the number 1101 by 10, quotient will be 110 and remainder will be 1. The remainder 1 is added to queue (it will be stored at rear=0) i.e. at location queue (see Figure 1). After adding an element to the queue, value of rear is incremented by 1. The quotient 110 is again divided by 10, the quotient will be 11 and remainder will be 0. The remainder 0 is added to queue (at rear=1) i.e. at queue location. The quotient 11 is divided by 10, the quotient will be 1 and remainder too will be 1. The remainder 1 is added to queue at rear=2 location i.e. at queue location. Because, the quotient 1 is less than 10, it can’t be divided by 10 and is hence added to queue at rear=3 location i.e. at queue location. Figure 1 – Splitting binary number into bits by dividing it by 10

The second step is take out the separated bits from the queue and multiply each of them by powers of 2 i.e. 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 first  binary digit in the queue) is multiplied by 20, 0 the second binary digit in queue 1 is multiplied by 21, the next digit in queue i.e. 1 is multiplied by 22 and the last digit in queue, 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 – Values taken out from queue and each digit is multiplied by powers of 2

C Program to Convert Binary Number to Decimal Number

# include <stdio.h>

# include <math.h>

#define max 10

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

int front=-1,rear=-1;

void addq(int queue[], int value);

int delq(int queue[]);

main()

{

int q[max],k,binnumb, remainder, temp, decnumb=0, power=0;

printf(“Enter a number in binary “);

scanf(“%d”,&binnumb);

/* 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=binnumb;

while(binnumb >0)

{

remainder=binnumb%10;

binnumb=binnumb/10;

}

printf(“The decimal conversion of the binary number %d is “, temp);

k=delq(q);

if(k!=’#’)

{

while(k!=’#’)

{

decnumb=decnumb+k*pow(2,power);

power++;

k=delq(q);

}

}

printf(“%d”, decnumb);

}

void addq(int queue[], int value)

{

rear++;

queue[rear]=value;

if(front==-1)

front=rear;

}

int delq(int queue[])

{

int j;

if(front==-1)

{

//Queue is empty

return ‘#’;

}

else

{

if(front==rear)

{

j=queue[front];

front=-1;

rear=-1;

}

else

{

j=queue[front];

front++;

}

return(j);

}

}