Tuesday , 24 January 2017
Home » Class 12 » Program to Check for Kaprekar Number

Program to Check for Kaprekar Number

Question:

Write a Program in Java to input a number and check whether it is a Kaprekar number or not.

Note: A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.

If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ……..

Example 1: 9

92  = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8

Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.

Example 2: 45

452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20

Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number.

Example 3: 297

297= 88209, right-hand piece of 88209 = 209 and left hand piece of 88209 = 88

Sum = 209 + 88 = 297, i.e. equal to the number. Hence, 297 is a Kaprekar number.

Solution:

/**
* The class Kaprekar inputs a number and checks whether it is a Kaprekar Number or not
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Kaprekar
{
   public static void main(String args[]) throws IOException
   {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.print("Enter a Number : ");
     int n = Integer.parseInt(br.readLine()); //Inputting the number

     int sq = n*n; //finding the square of the number  
     String s = Integer.toString(sq); //converting the square into a String

     if(sq<=9)
        s = "0"+s; //Adding a zero in the beginning if the square is of single digit

     int l = s.length(); //finding the length (i.e. no. of digits in the square).
     int mid = l/2; //finding the middle point

     String left=s.substring(0,mid); //extracting the left digits from the square
     String right=s.substring(mid); //extracting the right digits from the square

     int x = Integer.parseInt(left); //converting the left String into Integer
     int y = Integer.parseInt(right); //converting the right String into Integer

     //if sum of left and right numbers is equal to the original number then it is a Kaprekar number
     if(x+y == n) 
        System.out.println(n+" is a Kaprekar Number");
     else
        System.out.println(n+" is Not a Kaprekar Number");
   }
}

Output:

Enter a Number : 297
297 is a Kaprekar Number

Alternate Code which can be used:

Code Used Alternate Code
String left = s.substring(0,mid); //extracting the left digits from the square
String right = s.substring(mid); //extracting the right digits from the square
int x = Integer.parseInt(left); //converting the left String into Integer
int y = Integer.parseInt(right); //converting the right String into Integer
int digleft = mid; //finding the no. of digits on the left
int digright = l-mid; //finding the no. of digits on the right
int x = sq/(int)Math.pow(10,digright); //extracting the left digits from the square
int y = sq%(int)Math.pow(10,digright); //extracting the right digits from the square

Explanation:

We have solved the above program through the following steps:

1. We first enter a number and find its square.

2. In order to find the number of digits in its square, we smartly convert the square into a String. Then we check whether the square is a single digit number or not. If it is a single digit number, then we add a “0” infront of the String storing the square. this is because Kaprekar numbers like 1, whose square is also a single digit number i.e. 1, will have their left digit as ‘0’.

3. Then we use the length() function to find its length. In this way we have the number of digits in the square without even writing those confusing while loops. Smart isn’t it !

[Note: We used the Wrapper class function for converting from an integer to a String and vice versa.

To convert an integer to String, you just need to use this syntax Integer.toString(number); where ‘number‘ is the integer you want to convert into String.

The same goes for values of type double, float etc. You just have to use Double.toString(number) to convert a double type number into a String. [Note: This trick has many advantages]]

4. After finding the length of the String i.e. the number of digits in the square of the number, we find the middle position from where we will be dividing the square into left part and right part.

5. Then we use the substring function of the String class to extract the left string starting from 0 till mid position and similarly, extracting the right string from mid till the last position.

6. Then we convert these two left and right Strings into Integers, by using Wrapper class function parseInt().

[Note: To convert a String into an Integer, you just need to use this index: Integer.parseInt(value), where ‘value‘ is the String you want to convert into an Integer.]

7. Finally we check whether the sum of left and right numbers is equal to the original number or not. If they are equal, then it is a Kaprekar number, else it is not.

Working:

If the number entered is, n = 297, then

int sq = n*n;
sq = 297 * 297
sq = 88209

String s=Integer.toString(sq);
String s=Integer.toString(88209);
String s = “88209”

int l = s.length();
l = 5

int mid = l/2;
mid = 5/2
mid = 2

String left=s.substring(0,mid);
left = s.substring(0,2);
left = “88”

String right=s.substring(mid);
right = s.substring(2);
right = “209”

int x=Integer.parseInt(left);
x = Integer.parseInt(“88”);
x = 88

int y=Integer.parseInt(right);
y = Integer.parseInt(“209”);
y = 209

Now, the condition (x+y == n) i.e. 88+209 == 297 is satisfied, hence, 297 is a Kaprekar Number.

Check Also

ISC 2016 Computer Science Solution + Examiner’s Comments – From the Council

Solution of ISC 2016 Computer science Paper as provided by the Council for the Indian School Certificate Examinations.

8 comments

  1. Its an awesome site………..mind blowing………no words to explain it goodness!

  2. EASY FOR STUDENTS TO UNDERSTAND…..
    I HAVE BEEN CLEARED ABOUT MY TOPICS!!!

  3. I really love this code. It has really saved me from a lot of stress.

  4. Thankyou !!

  5. this webpage helps me in completing my project!!

  6. this made me easy to understand thank you

  7. This is the easiest code i have ever seen.It is more simpler than mine.I did the program without using a string.

  8. heartsword27@gmail.com

    This is an awesome code! Short and simple to understand…thank you so much! 😀

Leave a Reply to Maruthi mahadev Cancel reply

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