Check Kaprekar Number in Java
Problem
A Kaprekar number is one whose square can be split into two parts — a right part with as many digits as the original number, and a left part with the rest — that add back up to the original number.
Given a number, determine whether it is a Kaprekar number.
Java Program
public class KaprekarNumberCheck {
public static void main(String[] args) {
int n = 45;
long square = (long) n * n; // long avoids overflow for larger n
String squareStr = String.valueOf(square);
int digits = String.valueOf(n).length();
// Split the squared string into a right part (last `digits` characters) and a left part (the rest)
String rightStr = squareStr.substring(squareStr.length() - digits);
String leftStr = squareStr.substring(0, squareStr.length() - digits);
long right = Long.parseLong(rightStr);
long left = leftStr.isEmpty() ? 0 : Long.parseLong(leftStr);
boolean isKaprekar = right != 0 && (left + right == n);
System.out.println(n + " is a Kaprekar number: " + isKaprekar);
}
}Output
Core Logic
Squaring the number, converting the result to a string, and slicing off exactly as many digits from the right as the original number has, splits the square into the two parts the definition asks for.
- 1
squareholdsn * n, computed as alongto avoid overflow for larger inputs. - 2
digitscounts how many digitsnitself has, which determines where the split happens. - 3
rightStrtakes the lastdigitscharacters of the squared string, andleftStrtakes everything before that. - 4Both pieces are parsed back into numbers, and
left + right == nchecks whether they add up to the original.
45, the square is 2025; splitting off the last 2 digits gives 20 and 25, and 20 + 25 = 45 confirms it.Key Point: The right != 0 check matters — a split like 1 and 00 (which is just 0) doesn't count as a genuine two-part split, so numbers that degenerate this way are correctly excluded.
Why: Converting the square to a string and slicing it costs time proportional to its digit count, and the substrings themselves are held temporarily.
Key Concepts
Approach 2: Arithmetic (No Strings)
public class KaprekarNumberCheckArithmetic {
public static void main(String[] args) {
int n = 45;
long square = (long) n * n;
int digits = String.valueOf(n).length();
long divisor = (long) Math.pow(10, digits);
long right = square % divisor; // last `digits` digits of the square
long left = square / divisor; // everything before that
boolean isKaprekar = right != 0 && (left + right == n);
System.out.println(n + " is a Kaprekar number: " + isKaprekar);
}
}
Output
Core Logic
The same split can be computed with pure arithmetic — dividing and taking the remainder by the right power of 10 does exactly what slicing a string does, without ever building one.
- 1
divisoris10raised to the number of digits inn, computed withMath.pow(). - 2
square % divisorisolates the lastdigitsdigits of the square — the same value the string version'srightStrcaptured. - 3
square / divisordiscards those same digits, leaving the left part — the same valueleftStrcaptured. - 4The rest of the check —
right != 0andleft + right == n— stays identical to the string-based version.
45, divisor is 100; 2025 % 100 = 25 and 2025 / 100 = 20, the same split the string version found.Key Point: This avoids allocating any String at all — the split happens purely through division and remainder, which is why it's the version to reach for if this check runs inside a tight loop.
Why: The right and left parts are computed with pure integer arithmetic, without ever allocating a String.