Question

Write a program that prompts the user to input a binary number and display its decimal equivalent.

Source Code

#include <stdio.h>
#include <math.h>

int main()
{
    int binary, n, remainder, decimal = 0, i = 0;

    printf("Enter a binary number :");
    scanf("%d", &binary);

    n = binary;

    while (n > 0)
    {
        remainder = n % 10;
        decimal += remainder * pow(2, i++);
        n /= 10;
    }
    
    printf("Decimal equivalent of %d is %d", binary, decimal);

    return 0;
}

Output

Enter a binary number :1100
Decimal equivalent of 1100 is 12