Before going to the program to convert Binary to Decimal first let us see how to convert binary to decimal.

Let us assume the Binary Number as 110101.

The Decimal Number is equal to the sum of powers of 2 of the binary number’s ‘1’ digits place:

Binary Number: | 1 | 1 | 0 | 1 | 0 | 1 |
---|---|---|---|---|---|---|

Power of 2: | 2 ^{5} |
2 ^{4} |
2 ^{3} |
2 ^{2} |
2 ^{1} |
2 ^{0} |

Thus the Decimal Number equivalent to the Binary Number 110101 is as given below:

110101_{2} = 1·2^{5}+1·2^{4}+0·2^{3}+1·2^{2}+0·2^{1}+1·2^{0} = 53

## Program code to convert Binary to Decimal in C:

/* Program to convert Binary to Decimal */ #include<stdio.h> #include<math.h> void main() { int n,bnum,digit,dec=0,i=0; clrscr(); printf("n Enter the binary number:"); scanf("%d",&bnum); n=bnum; /*Loop to calculate decimal number for given the binary number*/ while(n!=0) { digit=n%10; dec+=digit*pow(2,i); n=n/10; i++; } printf("n The binary equivalent of %d in decimal is: %d",bnum,dec); getch(); }

Related: **C program to convert Binary to Decimal using the Left Shift (<<) operator**

## Step by Step working of the above Program Code:

- Let us assume that the user enters the binary number as
**1011**. - It assigns the value of dec=0, i=0, bnum=1011.
- It assigns the n=bnum (ie.
**n=1011**) and the loop continues till the condition of the while loop is true.

3.1. n!=0 (1011!=0) while loop condition is true

digit=n%10 (digit=1011%10) So digit=1

dec+=digit*pow(2,i) (dec=0+1*pow(2,0)) So **dec=1**

n=n/10 (n=1011/10) So **n=101**

i++ (i=i+1) So i=2

3.2. n!=0 (101!=0) while loop condition is true

digit=n%10 (digit=101%10) So digit=1

dec+=digit*pow(2,i) (dec=1+1*pow(2,1)) So **dec=3**

n=n/10 (n=101/10) So **n=10**

i++ (i=i+1) So i=3

3.3. n!=0 (10!=0) while loop condition is true

digit=n%10 (digit=10%10) So digit=0

dec+=digit*pow(2,i) (dec=3+0*pow(2,2)) So **dec=3**

n=n/10 (n=10/10) So **n=1**

i++ (i=i+1) So i=4

3.4. n!=0 (1!=0) while loop condition is true

digit=n%10 (digit=1%10) So digit=1

dec+=digit*pow(2,i) (dec=3+1*pow(2,3)) So **dec=11**

n=n/10 (n=1/10) So **n=0**

i++ (i=i+1) So i=5

3.5. n!=0 (0!=0) while loop condition is false

It comes out of the while loop.

- Finally it prints The binary equivalent of
**1011**in decimal is:**11** - Thus the program execution is completed.