Question: 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
