Kaprekar Number Program in Java

In this tutorial we will see what is kaprekar number , How to find kaprekar number . We will write a program to check given number is kaprekar number in java and also we write java program to print all kaprekar number in given range .

What is Kaprekar Number?

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(given number) then it is called kaprekar number. The numbers are 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.

Algorithm to find kaprekar number

First we will write algorithm to calculate kaprekar number . Let’s discuss algorithms step  by step

  1. Take input from user an store in variable (number).
  2.  Calculate square of number and store in variable square .
  3.  Count digit of square and store in countDigits .
  4.  Now iterate  i =countDigits-1 to i > 0
  5.  Calculate first part by  square divide by 10^i
  6.  Calculate second part is remainder value  , when square divide by 10^i .
  7.  If any part is 0 then go next iteration.
  8.  Calculate sum of both  part
  9.  If sum is equal to given number then given number is kaprekar number and return true .

kaprekar number in java

We implement this algorithm and write program to find kaprekar number in Java.

import java.util.Scanner;

public class KaprekarNumber {
	
	
	   //check given number is kaprekar or not
	   public static boolean isKaprekar(int number) {

		      int square = 0;
		      int temp = 0;
		      int countDigits = 0;
		      int firstPart = 0;
		      int secondPart = 0;
		      int sum = 0;

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

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

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

		         // find first part of square
		         firstPart = square / (int)Math.pow(10, i);
		         // find second part of square
		         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 from user
		      Scanner scan =  new Scanner(System.in);
		      System.out.print("Enter a number : ");
		      number = scan.nextInt();

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

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

Output :

Enter a number : 45
Given 45 is a kaprekar number

Java Program to print all kaprekar number in a given range

Now we write program to find all kaprekar number in java in between a given range . We take input of min and max range from user and then calculate kaprekar number in given range .

import java.util.Scanner;

public class KaprekarNumberInRange {

	   //check given number is kaprekar or not
	   public static boolean isKaprekar(int number) {

		      int square = 0;
		      int temp = 0;
		      int countDigits = 0;
		      int firstPart = 0;
		      int secondPart = 0;
		      int sum = 0;

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

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

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

		         // find first part of square
		         firstPart = square / (int)Math.pow(10, i);
		         // find second part of square
		         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 for min range and max range
			     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 is kaprekar number or not
			     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();
		   }
		   
}

Output :

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

Here , we print all Kaprekar numbers between 1 to 100

In this tutorial we learned what is kaprekar number in java and written java program to find kaprekar number.  We also written java program to print all kaprekar number in a range .

Leave a Reply

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

7 + 2 =