Kaprekar Number Program in Java

In this post we will develop a Java program to check the given number is kaprekar number or not. Later we will also develop a Java program to find all the kaprekar numbers in the given range.

A number whose square divided into two parts (none of the parts have only 0) and the sum of the parts is equal to the original number then it is called kaprekar number. The kaprekar number is named after D. R. Kaprekar.

Example of kaprekar numbers:- 45
The square of 45 = 2025
20 + 25 = 45, so 45 is a kaprekar number.

10, 100, 1000 are not a kaprekar number
10^2 = 100 => 10 + 0= 10
100^2 = 10000 = 100 + 00 = 100
But the condition is none of the part having only 0, so these are not the kaprekar number.

Procedure to check the given number is kaprekar number or not

1) Take a number.
2) Find its square value.
3) Count the number of digits in the square.
4) Take a iterator variable (i) starting from number_of_digits_in_square – 1
5) Find the first part of the number, as dividing the square value by 10^i
6) Find the second part as the calculating remainder value after dividing 10^i
7) Check any part having only 0, if yes then skip the iteration.
8) Calculate the sum of both parts.
9) Compare sum and number, if both are equal then the given number is kaprekar number.

Java program to check number is kaprekar number or not

import java.util.Scanner;

public class KaprekarNumber {

   public static boolean isKaprekar(int number) {

      // declare variables
      int square = 0;
      int temp = 0;
      int countDigits = 0;
      int firstPart = 0;
      int secondPart = 0;
      int sum = 0;

      // calculate square value of the number
      square = number * number;

      // count number of digits in the square
      temp =square;
      while(temp!=0) {
         countDigits++;
         temp /= 10; 
      }

      // divide square into two parts and 
      // check it's sum is equal to the number?
      for(int i=countDigits-1; i>0; i--) {

         // find first part
         firstPart = square / (int)Math.pow(10, i);
         // find second part
         secondPart = square % (int)Math.pow(10, i);

         // check have any part only 0
         if(firstPart == 0 || secondPart == 0)
         	continue;
         // find sum value
         sum = firstPart + secondPart;

         // compare sum and number
         if( sum == number )
         return true;
      }
      return false;
   }

   public static void main(String[] args) {

      // declare variables
      int number = 0;

      // read the input 
      Scanner scan =  new Scanner(System.in);
      System.out.print("Enter an integer number:: ");
      number = scan.nextInt();

      // check the number is kaprekar number or not
      if(isKaprekar(number))
      System.out.println(number+" is a"
		    	+ " kaprekar number");
      else
      System.out.println(number+" is not a"
	      	  	+ " kaprekar number");

      // close Scanner class object
      scan.close();
   }
}

The output for the different test-cases:-

Enter an integer number:: 45
45 is a kaprekar number

PrepBytes
Use Code KNOWPROGRAM10 to get FLAT 10% OFF on all Courses

Enter an integer number:: 55
55 is a kaprekar number

Enter an integer number:: 25
25 is not a kaprekar number

Also see:- Special numberMagic numberArmstrong numberPerfect numberEvil NumberSpy NumberSunny number in Java

Java Program to find all kaprekar number in a given range

Using loops we can also find all the kaprekar numbers in a given range. The below program demonstrates it.

import java.util.Scanner;

public class KaprekarNumberInRange {

  public static boolean isKaprekar(int number) {

     // declare variables
     int square = 0;
     int temp = 0;
     int countDigits = 0;
     int firstPart = 0;
     int secondPart = 0;
     int sum = 0;

     // calculate square value of the number
     square = number * number;

     // count number of digits in the square
     temp =square;
     while(temp!=0) {
        countDigits++;
        temp /= 10; 
     }

     // divide square into two parts and 
     // check it's sum is equal to the number?
     for(int i=countDigits-1; i>0; i--) {

        // find first part
        firstPart = square / (int)Math.pow(10, i);
        // find second part
        secondPart = square % (int)Math.pow(10, i);

        // check have any part only 0
        if(firstPart == 0 || secondPart == 0)
        continue;

        // find sum value
        sum = firstPart + secondPart;

        // compare sum and number
        if( sum == number )
        return true;
     }
     return false;
  }

  public static void main(String[] args) {

     // declare variables
     int minRange = 0, maxRange = 0;

     // create Scanner class object
     Scanner scan = new Scanner(System.in);
     // read inputs
     System.out.print("Enter min value of range:: ");
     minRange = scan.nextInt();
  
     System.out.print("Enter max value of range:: ");
     maxRange = scan.nextInt();

     // check number 
     System.out.println("The kaprekar numbers from "+
          minRange+" to "+ maxRange+" are:: ");

     for(int i=minRange; i<=maxRange; i++) {
        if(isKaprekar(i))
	   System.out.print(i+" ");
     }

     // close Scanner class object
     scan.close();
  }
}

The output for the different test-cases:-

Enter min value of range:: 1
Enter max value of range:: 100
The kaprekar numbers from 1 to 100 are::
9 45 55 99

Enter min value of range:: 100
Enter max value of range:: 100000
The kaprekar numbers from 100 to 100000 are::
297 703 999 2223 2728 4879 4950 5050 5292 7272 7777 9999 17344 22222 38962

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *