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.
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
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
sum+=right_n_digits; }//outer loop ends
System.out.println(n+ ” is a karpekar number”);
System.out.println(n + “is not a karpekar number”);
}// method ends
public int findLength(int n)
int rem= n%10;
Now you can write a code to print first 20 karpekar numbers or karpekar numbers within the given range.
For more such programs, click here: LINK
For understanding arrays, click here: LINK