In mathematics, a Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 45² = 2025 and 20+25 = 45. The Kaprekar numbers are named after D. R. Kaprekar.

There are a lot of code examples on the internet which deal with computing Kaprekar Numbers. One of the methods is to compute the quotient and dividend then add them to see if its Kaprekar. The problem with this approach is it incorrectly says number like 4879, 5272 etc to be non Kaprekar’s number.

Here is a solution which computes Kaprekars Number(Base 10), this solution does not miss out on numbers like 4879,5252 etc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class Kaprekar{
	public static void main(String[] args){
                //Computing upto a million numbers, 
                //can increase it to your liking or
                //use scanner api to take input from keyboard
		for(long i=1;i<1000000;i++){
			isKaprekarNum(i);
		}
	}
        // This method works by splitting the
        // the squared number into half,
        // then adding the two parts. If two parts sum is
        // the input number we decide its kaprekar number, else
        // we move the pivot to the left and do the operation again until
        // its kaprekar or we run to beginning of number.
	public static boolean isKaprekarNum(long num){
		boolean isKaprekar = false;
		String sqr = ""+(num*num);
		int length = sqr.length();
		int mid = length/2;
		long tmp1=0,tmp2=0;
		String tmp1Str,tmp2Str;
		for(int i=mid;i>=0;i--){
			tmp1Str = sqr.substring(0,i);
			tmp2Str = sqr.substring(i,length);
			tmp1 = convertStrToLong(tmp1Str);			
			tmp2 = convertStrToLong(tmp2Str);
			if(num == tmp1+tmp2){
				System.out.println(num+"="+tmp1Str+"+"+tmp2Str);
				System.out.println("----------------------");
				isKaprekar =  true;
				break;
			}
		}
		return isKaprekar;
	}
 
	public static long convertStrToLong(String str){
		long rtrn = 0;
		try{
			rtrn = Long.parseLong(str);
		}catch(NumberFormatException e){
			//hide it under carpet and return 0
		}
		return rtrn;
	}
 
}

Here is the Output:

1=0+1
———————-
9=8+1
———————-
45=20+25
———————-
55=30+25
———————-
99=98+01
———————-
297=88+209
———————-
703=494+209
———————-
999=998+001
———————-
2223=494+1729
———————-
2728=744+1984
———————-
4879=238+04641
———————-
4950=2450+2500
———————-
5050=2550+2500
———————-
5292=28+005264
———————-
7272=5288+1984
———————-
7777=6048+1729
———————-
9999=9998+0001
———————-
17344=3008+14336
———————-
22222=4938+17284
———————-
38962=1518+037444
———————-
77778=60494+17284
———————-
82656=68320+14336
———————-
95121=90480+04641
———————-
99999=99998+00001
———————-
142857=20408+122449
———————-
148149=21948+126201
———————-
181819=33058+148761
———————-
187110=35010+152100
———————-
208495=43470+165025
———————-
318682=101558+217124
———————-
329967=108878+221089
———————-
351352=123448+227904
———————-
356643=127194+229449
———————-
390313=152344+237969
———————-
461539=213018+248521
———————-
466830=217930+248900
———————-
499500=249500+250000
———————-
500500=250500+250000
———————-
533170=284270+248900
———————-
538461=289940+248521
———————-
609687=371718+237969
———————-
627615=39390+0588225
———————-
643357=413908+229449
———————-
648648=420744+227904
———————-
670033=448944+221089
———————-
681318=464194+217124
———————-
791505=626480+165025
———————-
812890=660790+152100
———————-
818181=669420+148761
———————-
851851=725650+126201
———————-
857143=734694+122449
———————-
961038=923594+037444
———————-
994708=989444+005264
———————-
999999=999998+000001
———————-
Kaprekar Numbers in first million integers are :
1,9,45,55,99,297,703,999,2223,2728,4879,4950,5050,5292,7272,7777,9999,
17344,22222,38962,77778,82656,95121,99999,142857,148149,181819,187110,
208495,318682,329967,351352,356643,390313,461539,466830,499500,500500,
533170,538461,609687,627615,643357,648648,670033,681318,791505,812890,
818181,851851,857143,961038,994708,999999

References:
Online Encyclopedia Of Integer Sequences
Kaprekar Number Wikipedia Page

“Mathematics allows for no hypocrisy and no vagueness.”
-Stendhal