C program to convert Binary to Decimal

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:

1101012 = 1·25+1·24+0·23+1·22+0·21+1·20 = 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:

  1. Let us assume that the user enters the binary number as 1011.
  2. It assigns the value of dec=0, i=0, bnum=1011.
  3. 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.

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

Output:

binary to decimal

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

 

Leave a Reply

Your email address will not be published. Required fields are marked *