Kaprekar number

What is Kaprekar Number?
A number whose square when divided into 2 parts and each part is added, the result is equal to the number itself is called Kaprekar number.
Example,
Consider 99. Its square is 9801.
Its two parts are 98 and 01.
Their sum = 98 + 01 = 99 which is the same as the number.
Hence it is Kaprekar number.


Following things should be remembered about a Kaprekar number.

  • It is not necessary that two parts have same number of digits. If the sum of any two parts is equal to the number, then it is a Kaprekar number.
    Example, 297. Its square is 88209. Its two parts can be 8 and 8209; 88 and 209; 882 and 09; 9920 and 9.
    Sum of second combination = 88 + 209 = 297. Thus, it is a Kaprekar number.
  • Second part can begin with 0 but it can not be completely 0. Example, 100. Its square is 10000. Its two parts are 100 and 00(not allowed).

Check if a number is a Kaprekar number in java
In order to check if a given number is a Kaprekar number using java program, we need to write an algorithm and then convert it to the program.
Steps of the algorithm are

  1. Calculate square of the number.
  2. Calculate the length of square, that is, the number of digits in square.
  3. Iterate over the square and in every iteration, divide the square into two parts.
  4. Add both the parts and compare the sum with the original number.
  5. If the sum and number are both equal, then the number is Kaprekar number and terminate the loop.
  6. If sum and number do not match, then repeat steps 3 to 5 till all the digits of square are covered.

Java Program
Program to check if a given number is Kaprekar or not based on the above algorithm is given below followed by its explanation.

import java.util.Scanner;
 
public class KaprekarNumberTest {
 
   public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter a number");
	// read user input
	int number = scanner.nextInt();
	scanner.close();
	// check for Kaprekar number
	boolean checkKaprekar = new KaprekarNumberTest().checkKaprekar(number);
	if (checkKaprekar) {
		System.out.println(number + " is a Kaprekar number");
	} else {
		System.out.println(number + " is not Kaprekar number");
	}
   }
 
 
   boolean checkKaprekar(int n) {
	// calculate square of number
	int square = n * n;
	// make copy of square
	int squareCopy = square;
	int len = 0;
	// determine length of square of number
	while (squareCopy != 0) {
		len++;
		squareCopy /= 10;
	}
	boolean isKaprekar = false;
	// iterate over square
	for (int i = 1; i < len; i++) {
	   // divide square by 10, 100, 1000...etc
	   int divisor = (int) Math.pow(10, i);
	   // get left part of number
	   int quotient = square / divisor;
	   // get right part
	   int remainder = square % divisor;
	   // add left and right parts and compare it with number
	   if (quotient + remainder == n) {
	  	// it is Kaprekar number
		isKaprekar = true;
	   }
	}
	return isKaprekar;
   }
}

Explanation
Above program reads a number from keyboard using java.util.Scanner class and passes it on to a method which determines whether the supplied number is a Kaprekar number.

Learn how to read input from console in different ways here.

This method first calculates square of the number and creates its copy as well. Copy is required since the square will be modified to calculate its length.
If you do not want to keep the copy, then calculate the square again later when required.


To calculate the length of square, divide the square by 10 till the quotient is 0. Length of the square will be equalt to the number of times division is done. Also, update the square with the quotient else you will be stuck in an infinite loop.
Example, suppose 625 is the square value, then
625/10 = 62   (Division Count: 1)
62/10 = 6       (Division Count: 2)
6/10 = 0         (Division Count: 3)
Length of 625 is also 3.
Next, iterate from 1 till the length of the square. In every iteration, determine the left and right parts of the square.
For left part, divide the square with multiples of 10 such as 10, 100, 1000…
This is because when you divide a number by 10, then the resultant quotient is the left part of the number with rightmost digit removed. Example, 7998/10 = 799.
Similarly when a number is divided by 100, then the resultant quotient is the left part of the number with 2 right digits removed. Example, 7998/100 = 79.
For right part
, take the modulus(%) of the square with multiples of 10
When you take modulus of a number by 10, then the result is the rightmost digit of the number. Example, 7998 % 10 = 8.
Finally, add these two parts(obtained by division and modulus) and compare the sum with the number.
If both are equal, then return true which marks that the number is a Kaprekar number else check the same for other parts of the square.
If the sum of all parts does not match the number then return false.
Output

Enter a number
99
99 is a Kaprekar number
Enter a number
123
123 is not Kaprekar number


Method 2: Using String class
This method follows the same logic as above but it uses methods of java.lang.String class to determine the length of square and to get left and right parts of the number.
Code follows with explanation.

import java.util.Scanner;
 
public class KaprekarNumberTest {
 
   public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	System.out.println("Enter a number");
	// read user input
	int number = scanner.nextInt();
	scanner.close();
	// check for Kaprekar number
	boolean checkKaprekar = new KaprekarNumberTest().checkKaprekar(number);
	if (checkKaprekar) {
           System.out.println(number + " is a Kaprekar number");
	} else {
	   System.out.println(number + " is not Kaprekar number");
	}
   }
 
   boolean checkKaprekar(int n) {
	int square = n * n;
	// convert square to string and get its length
	int len = String.valueOf(square).length();
	// convert square to string
	String squareStr = String.valueOf(square);
	boolean isKaprekar = false;
	// iterate over the square
	for (int i = 2; i < len; i++) {
		// get left part from starting till i
		int left = Integer.parseInt(squareStr.substring(0, i));
		// get right part
		int right = Integer.parseInt(squareStr.substring(i));
		// check if the sum of both matches the number
		if (left + right == n) {
			isKaprekar = true;
		}
	}
	return isKaprekar;
   }
}

Explanation
This method is also based on the above algorithm but it utilizes methods from java.lang.String class.
It converts square of number into String format using String.valueOf method and calculates its length by calling length method on the resultant string.

Then, iteration is performed on this string. To calculate the left and right parts of the square, substring methods of String class are used.
substring with 2 arguments returns the string between the first argument and (second argument – 1). Thus, "7998".substring(0, 2) will return 7. This is why iteration is started from 2. This step will retrieve the left part of square.
substring with 1 argument returns the string from that index till the right end. Thus, "7998".substring(1) will return 998. This step will retrieve the right part of square.
Finally, these left and right parts are converted to integers, added and their sum is compared with the number to determine the result as above.
Output

Enter a number
297
297 is a Kaprekar number
Enter a number
983
983 is not Kaprekar number

If you want to determine all Kaprekar numbers between a range of numbers, then initiate a loop between that range and call the method that checks for Kaprekar number in every iteration.

0

Mark Your Impression

Close Menu