You are viewing a single comment's thread. Return to all comments →
This is my solution in C++:
void kaprekarNumbers(int p, int q) { int count = 0; for (int i = p; i <= q; i++) { long long num = static_cast<long long>(i) * i; string numString = to_string(num); int numLength = numString.length(); string leftStr = numString.substr(0, numLength - to_string(i).length()); string rightStr = numString.substr(numLength - to_string(i).length()); int left = (leftStr.empty()) ? 0 : stoi(leftStr); int right = (rightStr.empty()) ? 0 : stoi(rightStr); if (left + right == i) { cout << i << " "; count++; } } if (count == 0) { cout << "INVALID RANGE"; } }
Seems like cookies are disabled on this browser, please enable them to open this website
Modified Kaprekar Numbers
You are viewing a single comment's thread. Return to all comments →
This is my solution in C++: