Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I'm having an issue finding out why I can't get 4879 & 5292 to print without creating another else-if to pinpoint those two to divide
I'm having an issue finding out why I can't get 4879 & 5292 to print without creating another else-if to pinpoint those two to divide by a higher unit. Can someone help me figure out how to check zeros after dividing when adding numbers back together? All of the other Kaprekar numbers print except those. #include using namespace std; int main() { //declare variables int num, squaredNum, firstHalf, secondHalf, unit, test; //initialize number num = 1; cout << "Here are all Kaprekar numbers less than 10,000! "; cout << "----------------------------------------------- " << endl; //while loop while(num < 10000){ squaredNum = num * num; if (squaredNum < 100){ unit = 10; } else if (squaredNum < 10000){ unit = 100; } else if (squaredNum < 1000000){ unit = 1000; } else if (squaredNum >= 1000000){ unit = 10000; } //test if both sides add up to original number firstHalf = squaredNum/unit; //right side secondHalf = squaredNum%unit; //left side test = firstHalf + secondHalf; if (test == num){ cout << num << " is a Kaprekar! " << firstHalf; cout << " + " << secondHalf << " is = " << num << endl; //outputs kaprekar number num++; //increment number } 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