First of all we will talk about how binary number are converted back into decimal representation and later we will have program.
Here is the formula of this transformation:
Binary number: a3a2a1a0
Decimal number a x 23 + a x 22 + a x 21 + a x 20
Example:
Binary: 1101
Decimal: 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20 = 8 + 4 + 0 + 1 = 13
And here we have our program:
#include
#include
#include
int main() {
char str[100];
int ind;
int sum = 0;
printf("Please enter binary number: ");
scanf("%s", str);
for(ind = 0; ind < strlen(str); ind++) {
sum += (str[ind] - 0x30) * pow(2, strlen(str) - ind - 1);
}
printf("Number in decimal would be %d\n", sum);
return 0;
}
Testing:
Please enter binary number: 1101
Number in decimal would be 13
Please enter binary number: 10000001
Number in decimal would be 129
Please enter binary number: 11111111
Number in decimal would be 255
Please enter binary number: 0
Number in decimal would be 0
Copyright © 2026 eLLeNow.com All Rights Reserved.