ISC Practicals and Theory - Kaprekar Number
Updated 21 Oct 2026
A Kaprekar number is a number whose square, when split into two parts, gives parts that add up to the number itself. This program is written in the main method alone: it counts the digits, squares the number, splits the square and compares the sum of the two parts with the number.
Definition
Let $n$ be a number with $d$ digits. Find $n^2$ and split it into a right part of exactly $d$ digits and a left part made of the remaining digits (the left part is 0 if there are no remaining digits). If left part + right part $= n$, then $n$ is a Kaprekar number.
- $45$ has 2 digits: $45^2 = 2025$, right part $= 25$, left part $= 20$, and $20 + 25 = 45$. So 45 is a Kaprekar number.
- $9$ has 1 digit: $9^2 = 81$, right part $= 1$, left part $= 8$, and $8 + 1 = 9$. So 9 is a Kaprekar number.
- $297$ has 3 digits: $297^2 = 88209$, right part $= 209$, left part $= 88$, and $88 + 209 = 297$. So 297 is a Kaprekar number.
This program follows the usual rule that the right part must not be zero. Because of this, 10 is not a Kaprekar number: $10^2 = 100$, right part $= 00 = 0$, left part $= 1$, and $1 + 0 = 1$, which is not 10. (Without this rule, 10, 100, 1000 and so on would wrongly pass, since $10^2 = 100$ splits as 10 + 00.) The number 1 is a Kaprekar number: $1^2 = 1$, right part $= 1$, left part $= 0$, and $0 + 1 = 1$.
Algorithm
- Accept the number
n. Ifn <= 0, display an invalid input message and stop. - Copy
ntotempand count its digits indigits: whiletemp > 0, add 1 todigitsand dotemp = temp / 10. - Find the square
sq = (long) n * n. Thelongtype is used because the square of a largeintdoes not fit in anint. - Start with
div = 1and multiply it by 10 exactlydigitstimes, so thatdivbecomes $10^{digits}$. - Split the square:
right = sq % divis the lastdigitsdigits andleft = sq / divis the remaining digits. - If
right > 0andleft + right == n, display the square, the two parts and thatnis a Kaprekar number. Otherwise display that it is not.
Program
Everything is written inside main: the digit count, the square, the power of 10, the split and the final test.
import java.util.Scanner;
public class Kaprekar
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt(); // the number to be checked
if (n <= 0)
{
System.out.println("Invalid input. Enter a positive number.");
}
else
{
int temp = n, digits = 0; // digits counts the digits of n
while (temp > 0)
{
digits++;
temp = temp / 10;
}
long sq = (long) n * n; // the square of n
long div = 1; // 10 raised to digits
for (int i = 1; i <= digits; i++)
{
div = div * 10;
}
long right = sq % div; // the last digits of the square
long left = sq / div; // the remaining digits of the square
if (right > 0 && left + right == n)
{
System.out.println("Square = " + sq);
System.out.println("Left part = " + left + ", Right part = " + right);
System.out.println(n + " is a Kaprekar number.");
}
else
System.out.println(n + " is not a Kaprekar number.");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
n | int | The number to be checked |
temp | int | Working copy of n whose last digit is removed in each pass of the counting loop |
digits | int | Number of digits of n |
sq | long | The square of n |
div | long | The value $10^{digits}$, built by repeated multiplication by 10 |
i | int | Loop counter of the for loop that builds div |
right | long | The last digits digits of the square |
left | long | The remaining digits of the square |
Sample input and output
Sample 1
Enter a number: 45
Square = 2025
Left part = 20, Right part = 25
45 is a Kaprekar number.Sample 2
Enter a number: 10
10 is not a Kaprekar number.Sample 3
Enter a number: 1
Square = 1
Left part = 0, Right part = 1
1 is a Kaprekar number.Sample 4
Enter a number: -5
Invalid input. Enter a positive number.Dry run
Take n = 45. The counting loop gives digits = 2. Then the program continues as follows.
| Step | Statement | Value |
|---|---|---|
| 1 | sq = (long) n * n | 2025 |
| 2 | div after the for loop | 100 |
| 3 | right = sq % div | 25 |
| 4 | left = sq / div | 20 |
| 5 | right > 0 | true |
| 6 | left + right == n | $20 + 25 = 45$, true |
Both conditions are true, so the program prints the square, the two parts and that 45 is a Kaprekar number.
For the counting loop the trace is:
| Pass | temp (start) | digits | temp (end) |
|---|---|---|---|
| 1 | 45 | 1 | 4 |
| 2 | 4 | 2 | 0 |
The loop stops as temp is 0, and digits is 2.
Variations
- Kaprekar numbers in a range "Accept two numbers m and n and display all the Kaprekar numbers between m and n."
- Kaprekar numbers up to a limit "Display all the Kaprekar numbers from 1 to 1000."
- Using a method that returns boolean "Write a method that accepts an integer and returns true if it is a Kaprekar number, otherwise false."