Convert a given Binary number into Decimal number - C Language Programming

Convert a given binary number by user into a equivalent decimal number.



Click here to open this program in Turbo C++

/**********************************************************************************
 Statement - Convert the given binary number into decimal
 Programmer - Vineet Choudhary
 Written For - http://developerinsider.co
 **********************************************************************************/
#include <stdio.h>
#include <conio.h>

void main()
{
    int   num, bnum, dec = 0, base = 1, rem ;
    clrscr();
    
    printf("Enter a binary number(1s and 0s)\n");
    scanf("%d", &num);           /*maximum five digits */
    
    bnum = num;
    
    while( num > 0)
    {
        rem = num % 10;
        dec = dec + rem * base;
        num = num / 10 ;
        base = base * 2;
    }
    
    printf("The Binary number is = %d\n", bnum);
    printf("Its decimal equivalent is =%d\n", dec);
    
    getch();
} 			/* End of main() */

/*---------------------------------------------
 Output
 Enter a binary number(1s and 0s)
 10101
 The Binary number is = 10101
 Its decimal equivalent is =21
 ----------------------------------------------*/
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.