well I'm just stating this one and my brain is about racked w/ the # of assignments going on.. anyways. I have to write a program where a user will input a integer and it will determine the kaprekar #'s of that #, until it reaches the user # input.

Thing about kaprekar #'s is that you're comparing the results of the squaring, breaking it apart into two separate int's, suming; then making a comparison between the number prior to squaring and the sum of the two split int's.

ex.
9^2 = 81 , 8 + 1 = 9 ;
45^2 = 2025 , 20 + 25 = 45 ;
297^2 = 88209 , 88 + 209 = 297 ;

any idea on how to complete this part (splitting the squared result properly and summing)? Everything else should be rather simple.

well I'm just stating this one and my brain is about racked w/ the # of assignments going on.. anyways. I have to write a program where a user will input a integer and it will determine the kaprekar #'s of that #, until it reaches the user # input.

Thing about kaprekar #'s is that you're comparing the results of the squaring, breaking it apart into two separate int's, suming; then making a comparison between the number prior to squaring and the sum of the two split int's.

ex.
9^2 = 81 , 8 + 1 = 9 ;
45^2 = 2025 , 20 + 25 = 45 ;
297^2 = 88209 , 88 + 209 = 297 ;

any idea on how to complete this part (splitting the squared result properly and summing)? Everything else should be rather simple.

I would write a helper function:

void SplitNumberInTwo (int number, int& leftNum,
                  int& rightNum, int rightNumDigits);

So you pass it number and rightNumDigits and it splits number into two numbers, leftNum , and rightNum , which are passed by reference and changed in the function.

int number = 236453;
int leftNum, rightNum;
SplitNumberInTwo (number, leftNum, rightNum, 2);

Results are: leftNum contains 2364, rightNum contains 53. then you can do your adding. The divide operator ( / ), the mod operator ( % ), and the pow function will come in handy when writing the function. You could also take another approach and use the atoi or itoa functions instead, though at least one of them and possibly both are non-standard.

I'd use the modulus operator and the division operator in a loop, something like:

do {
  knum += num%10;
} while ( num/=10 );

I'd use the modulus operator and the division operator in a loop, something like:

do {
  knum += num%10;
} while ( num/=10 );

ahh.. good idea. thanx

I have to do this same problem, but i am not allowed to use any functions for writing the program. I was wondering if Anybody knew how I could seperate the squared number into left and right number.

EX) 9^2 = 81, Since 9 is 1 digit we must seperate the right most cluster with 1 digit aka 1 from 81 and then add it to the left digit, 8. Any ideas?

This article has been dead for over six months. Start a new discussion instead.