#include #include using namespace std; string decimalToBinary(string decimalString) { int decimalValue = stoi(decimalString); // Convert decimal string to integer if (decimalValue == 0) { return "0"; // Special case: input is "0" } string binaryString = ""; while (decimalValue > 0) { int remainder = decimalValue % 2; // Get the remainder (0 or 1) binaryString = to_string(remainder) + binaryString; // Prepend the remainder to the binary string decimalValue /= 2; // Divide the decimal value by 2 } return binaryString; } int main() { string str; cout << "Enter IP address" << endl; cin >> str; string res = ""; string temp = ""; // Initialize temp here for (auto c : str) { if (c == '.') { string bin = decimalToBinary(temp); int k = bin.size(); for (int i = k; i < 8; i++) { bin = '0' + bin; } res += bin; temp = ""; // Reset temp for the next octet } else { temp += c; } } // Convert and append the last octet string bin = decimalToBinary(temp); int k = bin.size(); for (int i = k; i < 8; i++) { bin = '0' + bin; } res += bin; cout << "Binary representation: " << res << endl; return 0; }