#include <stdio.h>
#include <math.h>
int main() {
int binary, decimal = 0, power = 0;
printf("Enter a binary number: ");
scanf("%d", &binary);
while (binary != 0) {
int digit = binary % 10;
decimal += digit * pow(2, power);
binary /= 10;
power++;
}
printf("The decimal equivalent is %d\n", decimal);
return 0;
}
Enter a binary number: 1010101
The decimal equivalent is 85
binary
to store the binary number, decimal
to store the decimal equivalent, and power
to keep track of the current power of 2.printf
and scanf
.while
loop to convert each digit of the binary number to decimal. The loop continues until the binary number becomes 0.%
, and then multiply it with 2 raised to the current power of 2 using the pow
function from the math library. We add this value to the decimal
variable.power
variable by 1 to move to the next digit.decimal
variable as the decimal equivalent using printf
.