A Kaprekar number is a non-negative integer whose square, when divided into two parts, adds up to the original number. While the concept sounds simple, implementing a Kaprekar number in Java helps developers understand important programming fundamentals like string manipulation, loops, number splitting, conditional logic, and handling edge cases.
This type of problem is commonly asked in coding interviews and academic exams because it tests both mathematical reasoning and practical coding skills.
In this guide, you’ll learn the definition of a Kaprekar number, step-by-step algorithm, complete Java program with explanation, example inputs and outputs, range-based implementation, and important edge cases to consider.
What is a Kaprekar Number?
A Kaprekar number is a non-negative integer whose square can be split into two parts that add up to the original number itself. In simple terms, if you square a number and divide the result into two sections, and the sum of those sections equals the number you started with, then it is called a Kaprekar number.
Mathematical Explanation
Let a number be n.
- Compute its square:
n2n^2n2 - Split the square into two parts:
- Left part (L)
- Right part (R)
- Left part (L)
- If:
L+R=nL + R = nL+R=n
Then n is a Kaprekar number.
The split can occur at any valid position in the square (except leaving one side completely empty). The right part can contain leading zeros when necessary.
Examples of Kaprekar Numbers

1️, Example: 9
92=819^2 = 8192=81
Split into:
- 8 and 1
8+1=98 + 1 = 98+1=9
9 is a Kaprekar number.
2️. Example: 45
452=202545^2 = 2025452=2025
Split into:
- 20 and 25
20+25=4520 + 25 = 4520+25=45
45 is a Kaprekar number.
3️. Example: 55
552=302555^2 = 3025552=3025
Split into:
- 30 and 25
30+25=5530 + 25 = 5530+25=55
55 is also a Kaprekar number.
Step-by-Step Algorithm to Check Kaprekar Number
Below is the clear and simple algorithm to check whether a number is a Kaprekar number in Java:
1️ Input the Number
Take an integer input n from the user.
2️ Calculate the Square
Compute the square of the number:
square=n×nsquare = n \times nsquare=n×n
Use long data type to prevent overflow for large numbers.
3️ Convert the Square to String
Convert the squared value into a string format.
This makes it easier to split the digits into two parts.
Example:
If n = 45
Then square = 2025
Converted to string → “2025”
4️ Split the Square
Try all possible split positions of the string (except leaving one side empty).
For “2025” possible splits are:
- “2” and “025”
- “20” and “25” ✔
- “202” and “5”
Each split should be checked.
5️ Convert Parts and Add
Convert both left and right parts back into numbers:
sum=leftPart+rightPartsum = leftPart + rightPartsum=leftPart+rightPart
6️ Compare the Result
If:
leftPart+rightPart=nleftPart + rightPart = nleftPart+rightPart=n
Then the number is a Kaprekar number.
Otherwise, it is not.
Special Case
If n = 1, it is automatically a Kaprekar number because:
12=11^2 = 112=1
Java Program to Check Kaprekar Number
Below is a complete and working Java program that checks whether a number is a Kaprekar number.
It uses the long data type to prevent integer overflow and takes input using Scanner.
| import java.util.Scanner; public class KaprekarNumberChecker { // Method to check if a number is Kaprekar public static boolean isKaprekar(int n) { // Special case: 1 is a Kaprekar number if (n == 1) { return true; } // Use long to prevent overflow for large numbers long square = (long) n * n; // Convert square to string String squareStr = String.valueOf(square); int length = squareStr.length(); // Try all possible split positions for (int i = 1; i < length; i++) { String leftPartStr = squareStr.substring(0, i); String rightPartStr = squareStr.substring(i); long leftPart = Long.parseLong(leftPartStr); long rightPart = Long.parseLong(rightPartStr); // Check if sum equals original number if (leftPart + rightPart == n) { return true; } } return false; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int number = scanner.nextInt(); scanner.close(); if (isKaprekar(number)) { System.out.println(number + ” is a Kaprekar number.”); } else { System.out.println(number + ” is not a Kaprekar number.”); } } } |
Explanation of the Code
Let’s break down how the Kaprekar number program works in Java. Each part of the logic plays an important role in determining whether the number satisfies the Kaprekar condition.
Calculating the Square
The first step in the program is calculating the square of the input number:
long square = (long) n * n;
Here, we explicitly cast n to long before multiplication. This prevents integer overflow when dealing with large numbers. If we used int, the square of a large value could exceed the maximum limit of the int data type.
For example:
- If n = 45
- Then square = 45 × 45 = 2025
This squared value is what we will analyze further.
Converting Square to String
Next, we convert the squared number into a string:
String squareStr = String.valueOf(square);
Why do we convert it to a string?
Because splitting digits is much easier using string methods like substring() than using mathematical division and modulus operations. Converting to a string allows us to easily separate the number at different positions.
Example:
- Square = 2025
- Converted to string → “2025”
Now we can split it at any index.
Splitting the Number
We try all possible split positions using a loop:
for (int i = 1; i < length; i++)
This loop ensures:
- The left part is not empty
- The right part is not empty
Inside the loop:
String leftPartStr = squareStr.substring(0, i);
String rightPartStr = squareStr.substring(i);
For “2025”, possible splits are:
- “2” and “025”
- “20” and “25”
- “202” and “5”
Each split is converted back to numbers:
long leftPart = Long.parseLong(leftPartStr);
long rightPart = Long.parseLong(rightPartStr);
Comparing the Sum
Finally, we check whether the sum of both parts equals the original number:
if (leftPart + rightPart == n)
If the condition is true:
- The number is a Kaprekar number.
- The method returns true.
If no valid split satisfies the condition after checking all possibilities:
- The method returns false.
Example Inputs and Outputs
Below are sample executions of the program to help you understand how it works in real scenarios.
Example 1
Input:
45
Output:
45 is a Kaprekar number.
Explanation:
45² = 2025
Split → 20 + 25 = 45
Since the sum equals the original number, 45 is a Kaprekar number.
Example 2
Input:
10
Output:
10 is not a Kaprekar number.
Explanation:
10² = 100
Possible splits:
- 1 + 00 = 1
- 10 + 0 = 10 (invalid split with empty or improper division)
No valid split gives a sum equal to 10 under Kaprekar rules.
Therefore, 10 is not a Kaprekar number.
Finding Kaprekar Numbers in a Range (1 to 100)
In coding interviews, you may be asked not just to check a single number, but to print all Kaprekar numbers within a given range.
We can reuse the isKaprekar() method and simply apply it inside a loop.
Modified Java Code (1 to 100)
| public class KaprekarRange { public static boolean isKaprekar(int n) { if (n == 1) { return true; } long square = (long) n * n; String squareStr = String.valueOf(square); int length = squareStr.length(); for (int i = 1; i < length; i++) { String leftPartStr = squareStr.substring(0, i); String rightPartStr = squareStr.substring(i); long leftPart = Long.parseLong(leftPartStr); long rightPart = Long.parseLong(rightPartStr); if (leftPart + rightPart == n) { return true; } } return false; } public static void main(String[] args) { System.out.println(“Kaprekar numbers between 1 and 100:”); for (int i = 1; i <= 100; i++) { if (isKaprekar(i)) { System.out.println(i); } } } } |
Expected Output
Kaprekar numbers between 1 and 100:
1
9
45
55
99
Explanation
- The for loop runs from 1 to 100.
- For each number, the program checks whether it satisfies the Kaprekar condition.
- If true, the number is printed.
- This approach makes the program reusable and efficient.
You can easily modify the range by changing the loop values (for example, 1 to 1000) depending on your requirement.
Edge Cases and Important Points
When implementing a Kaprekar number in Java, it’s important to handle special conditions correctly. Below are some key edge cases and technical considerations you should keep in mind.
1. 1 is a Kaprekar Number
The number 1 is a special case.
12=11^2 = 112=1
It satisfies the Kaprekar condition because:
- It can be considered as 0 + 1 = 1
In most implementations, we explicitly return true when n == 1 to avoid unnecessary splitting logic.
2️. Handling Large Numbers
When squaring a number, the result can become very large.
For example:
- If n = 50,000
- Then n² = 2,500,000,000
This value exceeds the maximum limit of the int data type in Java.
That’s why we use:
long square = (long) n * n;
Using long prevents overflow and ensures accurate computation for larger inputs.
3️. Leading Zeros in Splits
When splitting the square (after converting it to a string), the right part may contain leading zeros.
Example:
100² = 10000
Possible split → “100” and “00”
When converted back to numbers:
- “00” becomes 0
This is valid and should be handled correctly.
However, splits that result in an empty string are not allowed.
4️. Using long vs int
| Data Type | Range | Suitable For |
| int | −2,147,483,648 to 2,147,483,647 | Small numbers |
| long | Much larger range | Safe squaring of bigger numbers |
Since squaring increases the value rapidly, long is the safer choice when implementing a Kaprekar number checker.
Time and Space Complexity
Understanding the time and space complexity of the Kaprekar number program helps evaluate its efficiency, especially in coding interviews.
Time Complexity: O(d)
Here, d represents the number of digits in the square of the number.
Why is it O(d)?
- First, we calculate the square of the number → constant time operation.
- Then we convert the square into a string → takes O(d) time.
- Next, we iterate through all possible split positions using a loop that runs up to d – 1 times.
- Inside the loop, substring operations and number parsing are performed, each proportional to the length of the split parts.
Since the loop runs based on the number of digits in the square, the overall time complexity becomes:
O(d)
If the input number has n digits, its square will have at most 2n digits, so the complexity still scales linearly with digit length.
Space Complexity: O(d)
The space complexity depends mainly on:
- Storing the squared number as a string.
- Creating substrings during splitting.
Since we store and manipulate strings of length d, the space required is proportional to the number of digits in the square.
Therefore, the space complexity is:
O(d)
Frequently Asked Questions (FAQ)

Q1. Is 1 a Kaprekar number?
Yes, 1 is a Kaprekar number.
12=11^2 = 112=1
It can be considered as:
- 0 + 1 = 1
Since the sum equals the original number, 1 satisfies the Kaprekar condition.
Q2. Is 100 a Kaprekar number?
No, 100 is not a Kaprekar number.
1002=10000100^2 = 100001002=10000
Possible splits:
- 1 + 0000 = 1
- 10 + 000 = 10
- 100 + 00 = 100 (invalid due to improper split handling under strict Kaprekar rules in most implementations)
- 1000 + 0 = 1000
No valid split produces a sum equal to 100 according to the standard Kaprekar definition used in programming problems. Therefore, 100 is not a Kaprekar number.
Q3. Can we solve Kaprekar number without using String?
Yes, it is possible to solve the Kaprekar number problem without converting the square into a string.
Instead of string manipulation, you can:
- Use modulus (%) to extract digits.
- Use division (/) to split the number mathematically.
- Determine the correct divisor based on powers of 10.
This approach is slightly more mathematical and is sometimes preferred in interviews to demonstrate strong number manipulation skills.
Q4. What are Kaprekar numbers between 1 and 100?
The Kaprekar numbers between 1 and 100 are:
1
9
45
55
99
These are the only numbers within this range whose square can be split into two parts that add up to the original number.
Conclusion
A Kaprekar number is a fascinating mathematical concept where a number’s square can be split into two parts that add up to the original number. In this guide, we explored how to implement a Kaprekar number in Java using string manipulation, handled important edge cases, analyzed time and space complexity, and even learned how to print Kaprekar numbers within a range.
Problems like this are commonly asked in coding interviews because they test your understanding of loops, conditionals, number manipulation, and logical thinking. Mastering such problems strengthens your programming fundamentals and improves your problem-solving skills.









