Wednesday, July 3, 2013

Convert a Binary number to Decimal number without using Pow() or Arrays or Functions



#include <stdio.h>
int main(void)
{
    long int bin;
    int total = 0 ,dec,mod,base =1 ;


    printf("\nEnter a binary number(0's & 1's),I'll tell you its decimal : ");
    scanf("%ld",&bin);


    while ( bin != 0  )
    {
        mod = bin % 10;
        total = total + base * mod;
        base = base * 2 ;
        bin =  bin / 10;
    }


    printf("\nDecimal of %ld is %d",bin,total);
    return 0;
}

No comments: