I'm trying to print all Kaprekar Numbers between Given Range
As input the range is between --> 1 to 99999 <--
The Expected Output :
1 9 45 55 99 297 703 999 2223 2728 4950 5050 7272 7777 9999 17344 22222 77778 82656 95121 99999
but I got this output :
1 9 45 55 99 297 703 999 2223 2728 4950 5050 7272 7777 9999 17344 22222
I stay miss 3 output any idea I'm using C++.
My code :
#include <bits/stdc++.h>
#include <cmath>
using std::cout;
using std::cin;
using std::endl;
using std::pow;
int b = 0, a = 0;
int main(int argc, char** argv) {
int lower, upper, count = 0;
cin >> lower >> upper;
for (int i = lower; i <= upper; i++) {
int n = i, digits = 0;
while (n != 0) {
digits++;
n /= 10;
}
n = pow(i, 2);
digits = pow(10, digits);
b = n % digits;
a = (n - b) / digits;
if (a + b == i) {
cout << i << " ";
count++;
}
digits = 0;
a = 0;
b = 0;
}
if (count == 0) cout << "INVALID RANGE";
return 0;
}