However I would like to suggest another method of computing the decimal equivalent of a binary number, which has several advantages. Here is the code:
#include <stdio.h> #include <conio.h> int main() { printf("Input binary value:\n"); char c; double value = 0; while ((c = getch()) != 13) { printf("%c", c); value = value*2.0 + c - '0'; } printf("\nCorresponding decimal value:\n%.0lf\n", value); return 0; }
It has the advantage that you can convert larger binary values into decimal form, since you are not storing the binary number as an integer, rather you keep each digit the user inputs in a char variable. Also the result is stored inside a variable of type double to permit conversion of very large numbers.
Try inputting100101010000001011111001000000000
you should get a decimal value of 5000000000, which is higher than the maximum unsigned long int number.
Login