Java ProgramsNumbersCheck Kaprekar Number

Check Kaprekar Number in Java

intermediate·  Numbers  ·  Number Theory

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.

Input
45
Output
45 is a Kaprekar number: true

Java Program

Java
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

45 is a Kaprekar number: true

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.

How It Works
  1. 1square holds n * n, computed as a long to avoid overflow for larger inputs.
  2. 2digits counts how many digits n itself has, which determines where the split happens.
  3. 3rightStr takes the last digits characters of the squared string, and leftStr takes everything before that.
  4. 4Both pieces are parsed back into numbers, and left + right == n checks whether they add up to the original.
For 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.

Complexity
Time Complexity: O(log n)Space Complexity: O(log n)

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

String.substring()squaringdigit splitting

Approach 2: Arithmetic (No Strings)

Java
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

45 is a Kaprekar number: true

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.

How It Works
  1. 1divisor is 10 raised to the number of digits in n, computed with Math.pow().
  2. 2square % divisor isolates the last digits digits of the square — the same value the string version's rightStr captured.
  3. 3square / divisor discards those same digits, leaving the left part — the same value leftStr captured.
  4. 4The rest of the check — right != 0 and left + right == n — stays identical to the string-based version.
For 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.

Complexity
Time Complexity: O(log n)Space Complexity: O(1)

Why: The right and left parts are computed with pure integer arithmetic, without ever allocating a String.

Key Concepts

modulo arithmeticMath.pow()integer division

Related Programs