C Program to Convert Binary to Decimal | Binary to Decimal in C

C Program to Convert Binary to Decimal


C Program to convert Binary to Decimal Number:

Code

// C Program to convert binary to decimal
#include<stdio.h>
#include<math.h>

// function to convert binary to decimal
int convert(long long num)
{
    int i = 0, decimal= 0;
    
    //converting binary to decimal
    while (num!=0)
    {
        int digit = num % 10;
        decimal += digit * pow(2,i);

        num /= 10;
        i++;
    }
    return decimal;
}

// main program
int main()
{
    // long used rather than int to store large values
    // Ex : int wont store 111111111111 (12 digits) as
    // limit for int is 2147483647 (10 digits)
    long long binary;
    
    printf("Enter binary number: ");
    scanf("%lld", &binary);
    
    printf("%lld", convert(binary));
    
    return 0;