Home > Java, Number Programs > Kaprekar Numbers

## Kaprekar Numbers

Since last few years, one question on manipulation of numbers has been consistently asked in ISC (class 12th) board exam eg. questions based on Smith numbers, Prime factors, Perfect numbers, etc. So number based programs are always important for ISC students.

Here I have tried to answer a query by a student on Kaprekar numbers. First I explain what are kaprekar numbers followed by java program code in Bluej to check whether the number n entered by the user is kaprekar number or not.

What are Kaprekar Numbers:

Take a positive whole number n that has d number of digits. Take the square n and separate the result into two pieces: a right-hand piece that has d digits and a left-hand piece that has either d or d-1 digits. Add these two pieces together. If the result is n, then n is a Kaperekar number. Examples are 9 (92 = 81, 8 + 1 = 9), 45 (452 = 2025, 20 + 25 = 45), and 297 (2972 = 88209, 88 + 209 = 297).
The first 20 Kaprekar numbers according to this definition are 1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4950, 5050, 7272, 7777, 9999, 17344, 22222, 77778, 82656, 95121, and 99999.

Kaprekar numbers can also be defined by higher powers. For example, 453 = 91125, and 9 + 11 + 25 = 45. The first ten numbers with this property are: 1, 8, 10, 45, 297, 2322, 2728, 4445, 4544, and 4949. For fourth powers, the sequence begins 1, 7, 45, 55, 67, (100), 433, 4950, 5050, 38212, 65068. Notice that 45 is a Kaprekar number for second, third, and fourth powers (454 = 4100625, and 4 + 10 + 06 + 25 = 45) – the only number in all three Kaprekar sequences, up to at least 400,000.

QUESTION : Write a program in Java(Bluej) to enter the number (n) and power (p) and check n is a Kaprekar number or not.

PROGRAM:

public class KarpekarNumbers

{

public void findKarpekar(int n, int exp)

{

int len; // to find no. of digits in n

int num; // to find n raised to power of exp

int rem; // to find remainder

int p; // to generate powers to make right_n_digits

int right_n_digits; // to store len no. of right digits

int count; // to count no. of digits extracted from right

int sum=0; // to find sum of right_n_digits

num=(int)(Math.pow(n,exp)); //find n raised to exp and typecast to int

len=findLength(n); // call method findLength()

while(num!=0) // till first digit on the left is extracted

{

count=0;

right_n_digits=0;

p=1;

while(count<len && num!=0)

{

rem= num%10; //find remainder

num=num/10; //find quotient

right_n_digits = rem * p + right_n_digits; //make a number of right len digits

p*=10; // generate powers

count++; //count no. of digits extracted

} //inner loop ends

System.out.println(right_n_digits);

sum+=right_n_digits;      }//outer loop ends

if(sum==n)

System.out.println(n+ ” is a karpekar number”);

else

System.out.println(n + “is not a karpekar number”);

}//  method ends

public int findLength(int n)

{

int len=0;

while(n!=0)

{

int rem= n%10;

n=n/10;

len++;

}

return len;

}//method ends

}//class ends

For Practice:

Now you can write a code to print first 20 karpekar numbers or  karpekar numbers within the given range.

1. September 23, 2012 at 3:07 pm

HEY!! the above program doesnt work for 99999 even though its a karprekar number!
would this program be fine—>

import java.io.*;
class karprekar
{
long n,ul,ll;
int count=0;
void input()throws IOException
{
if(check(n)==1)
{
System.out.println(“THE NUMBER IS A KARPREKAR NUMBER”);
}
else
{
System.out.println(“NOT A KARPREKAR NUMBER”);
}
System.out.println(“ENTER UPPER AND LOWER LIMIT”);
System.out.println(“The range is “);
for(long i=ll;i<=ul;i++)
{
if(check(i)==1)
{
System.out.print(i+" ");
count++;
}
}
System.out.println();
System.out.println("The frequency is "+count);
}
long check(long n)
{
long len=(""+n).length();
long sq=n*n;
long k=sq%(long)(Math.pow(10,len));
long s=sq/(long)(Math.pow(10,len));
long l=(k+s);
if(l==n)
return 1;
else
return 0;
}
public static void main(String args[])throws IOException
{
karprekar ob=new karprekar();
ob.input();
}
}

2. September 5, 2012 at 4:32 am

What is java.util.*; ?

• September 5, 2012 at 9:33 am

Its a package in java containing some utility classes Here it has been included for Scanner class

3. March 23, 2012 at 12:52 pm

@Nav: Pragram is absolutely fine; tested it also. This question came in class 12 practical exam.
You must practice questions based on:
1. if-else-if
2. patterns using nested loop
3. single dimensional arrays
4. simple string manipulation
5. simple series
8. writing main() method and object creation
9. writing prgram code according to the given class specification

Also have an idea about using Exception Handling and Scanner class in program

4. March 23, 2012 at 11:04 am

Can you please tell me if there is anything logically wrong with this program? gonna write the ICSE Computer Applications Exam in 3 days….

import java.io.*;
public class Kaprekar
{public static void main(String args []) throws IOException
System.out.println(“input the number”);
int sqr=n*n;
//finding number of digits
int nd = 0;
int copy=sqr;
while(copy>0)
{copy/=10;
nd++;}
copy=sqr;
int dig[] = new int[nd];
//creating an array containing the digits
for(int i=nd-1;i>=0;i–)
{dig[i]= copy%10;
copy/=10;
}
//finding the middle index
int mid = nd/2;
//generating the two pieces
int n1=0;
int n2=0;
for(int i=0;i<mid;i++)
n1=n1*10+dig[i];
for(int i=mid;i<nd;i++)
n2=n2*10+dig[i];
//checking
if ((n1+n2)==n)
System.out.println("Yes, it is a Kaprekar number.");
else
System.out.println("No; it isn't a Kaprekar number.");
}}

5. February 16, 2012 at 9:07 am

import java.io.*;
public class q16
{
public void main () throws IOException
{
int n=289;
float r;
Math.sqrt(n);
if (n==r)
System.out.println(“Perfect square number”);
else
System.out.println(“Non Perfect square number”);
}
}
what is the problem with this program and please correct it….

• February 18, 2012 at 4:52 pm

@ ank: sqrt() returns a double value so r should be of type double. Also no need to import java.io as you are not taking user inputs in this program. So Correction would be:

public class q16
{
public void main ()
{
int n=289;
double r=Math.sqrt(n);
if (n==r)
System.out.println(“Perfect square number”);
else
System.out.println(“Non Perfect square number”);
}
}

6. December 1, 2011 at 8:47 pm

WHAT IS THE PROBLEM WITH THIS PROGRAM

import java.util.*;
public class kar
{
public void karp()
{
Scanner in=new Scanner(System.in);
double n=in.nextDouble();
// double n=9;
double sq=n*n,t=n;
int c=0;
double k=sq;

while(sq!=0)
{ c++;
sq=sq/10;
}

for(int i=1;i<c;i++)
{ double r=k;
double r1=r%(int)(Math.pow(10,i));
double r2=r/(int)(Math.pow(10,i));

double sum=r1+r2;

if(sum==t)
{ System.out.println("karprekar");
}
else
{ System.out.println("Not karprekar");
}
}
}
}

• December 2, 2011 at 3:29 pm

@Bill: The given program code has a number of problems.

1. Kaprekar numbers are whole numbers so should be inputted as int type.

2. since you are inputting n as type double, finding the number of digits by division method is leading to infinity. eg. 45.0/10 repeatedly till quotient does not become 0 will go on.

3. the logic for extracting digits from the square of n is also wrong. You are initialing r=k within the loop. This will take out one digit, then two digits, then three from the original number, instead of the remaining quotient. Instead you are supposed to take out number of digits from sq equal to the length of n.

So correcting your logic, it would be:

import java.util.*;
public class kar
{
public void karp()
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();

int sq=n*n,t=n;
int c=0;
int r1=0,r2=0,sum=0;

while(n!=0)
{ c++;
n=n/10;
}

while (sq!=0)
{
r1=sq%(int)(Math.pow(10,c));
sq=sq/(int)(Math.pow(10,c));
sum=sum+r1;
}
if(sum==t)
System.out.println(“karprekar”);

else
System.out.println(“Not karprekar”);
}
}

7. February 20, 2011 at 7:52 pm

@ Manish: Though this problem can be solved using strings also but avoid using strings when manipulating numbers. eg. reversing numbers, decimal to binary conversion and vice-versa, etc can be done using strings but generally we do not do that way. Number manipulation requires that you solve it numerically unless it is not possible without strings or you cannot think of any other logic.