Question
c++: Kaprekar number test loop (list all Kaprekar numbers under 10,000) For some reason, I'm missing numbers between 9 and 2223..... #include using namespace std;
c++: Kaprekar number test loop (list all Kaprekar numbers under 10,000)
For some reason, I'm missing numbers between 9 and 2223.....
#include
using namespace std;
int main(){
//declare variables
int num, squaredNum, firstHalf, secondHalf, unit, test;
//initialize number
num = 1;
//while loop
while(num < 10000){
squaredNum = num * num;
if (squaredNum < 100){
unit = 10;
}
else if (squaredNum < 1000){
unit = 100;
}
else if (squaredNum < 10000){
unit = 1000;
}
else if (squaredNum < 100000){
unit = 10000;
}
firstHalf = squaredNum/unit;
secondHalf = squaredNum%unit;
test = firstHalf + secondHalf;
if (test == num){
cout << num << " is a kaprekar number!" << endl;
num++; //increment number
}else {
num++; //increment & continue to next number
}
}//end while loop
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started