Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Accept the number n. If n <= 0, display an invalid input message and stop.
  2. Copy n to temp and count its digits in digits: while temp > 0, add 1 to digits and do temp = temp / 10.
  3. Find the square sq = (long) n * n. The long type is used because the square of a large int does not fit in an int.
  4. Start with div = 1 and multiply it by 10 exactly digits times, so that div becomes $10^{digits}$.
  5. Split the square: right = sq % div is the last digits digits and left = sq / div is the remaining digits.
  6. If right > 0 and left + right == n, display the square, the two parts and that n is 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
nintThe number to be checked
tempintWorking copy of n whose last digit is removed in each pass of the counting loop
digitsintNumber of digits of n
sqlongThe square of n
divlongThe value $10^{digits}$, built by repeated multiplication by 10
iintLoop counter of the for loop that builds div
rightlongThe last digits digits of the square
leftlongThe 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.

One value per line

Dry run

Take n = 45. The counting loop gives digits = 2. Then the program continues as follows.

StepStatementValue
1sq = (long) n * n2025
2div after the for loop100
3right = sq % div25
4left = sq / div20
5right > 0true
6left + 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:

Passtemp (start)digitstemp (end)
14514
2420

The loop stops as temp is 0, and digits is 2.

Variations

  1. Kaprekar numbers in a range "Accept two numbers m and n and display all the Kaprekar numbers between m and n."
  2. Kaprekar numbers up to a limit "Display all the Kaprekar numbers from 1 to 1000."
  3. Using a method that returns boolean "Write a method that accepts an integer and returns true if it is a Kaprekar number, otherwise false."