Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Friday 24 October 2014

C program to convert binary number to decimal

Given below is a c program to convert binary number to decimal.

Example :

1111 - Binary Number
1        1        1        1  
23 22 21 20
8 4 2 1
23x1 + 22x1 + 21x1 + 20 = 8+4+2+1 = 15

101 - Binary Number
1        0        1  
22 21 20
4 2 1
22x1 + 21x0 + 20 = 4+0+1 = 5

Links that you may find important :

http://www.wikihow.com/Convert-from-Binary-to-Decimal
http://www.binaryhexconverter.com/binary-to-decimal-converter

Code :
#include <stdio.h>
#include <conio.h>
void main()
{    
    long int n,i=1,d=0,m=0,l=0;
    clrscr();
    printf("Enter binary number : ");
    scanf("%ld",&n);
    while(n>0)
    {
        m=(n%10);
        n=(n/10);
        l=m*i;
        d=d+l;
        i=i*2;
    }
    printf("Decimal equivalent is : %ld",d);
    getch();
}
Output :
Enter binary number : 1111
Decimal equivalent is : 15
Note : The program above has been tested using TurboCPP. Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.