#include #include #include // Function to check if input contains only '0' and '1' bool isValidBinary(const char *binary) { int length = strlen(binary); for (int i = 0; i < length; i++) { if (binary[i] != '0' && binary[i] != '1') { return false; // Return false if any character is not '0' or '1' } } return true; // Return true if all characters are '0' or '1' } // Function to convert binary number to decimal int bin2dec(const char *binary) { int length = strlen(binary); // Calculate the length of the binary string int decimal = 0; int power = 1; // Power of two for (int i = length - 1; i >= 0; i--) { if (binary[i] == '1') { decimal += power; // Add to the decimal value if the bit is '1' } power *= 2; // Increase the power of two for the next bit (power is ... multiplication) } return decimal; } int main() { char binary[32]; printf("Enter a binary number to convert to decimal: "); scanf("%s", binary); if (!isValidBinary(binary)) { printf("Invalid input! The input should contain only '0' and '1'.\n"); return 1; // Exit the program with an error code } int decimal = bin2dec(binary); printf("Decimal equivalent: %d\n", decimal); return 0; }