Given the two positive integers p and q, where p < q. Write a program to determine how many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and output them.About 'kaprekar' number:
A positive whole number 'n' that has 'd' number of digits is squared and split into 2 pieces, a right hand piece that has 'd' digits and a left hand piece that has remaining 'd' or 'd-1' digits. If sum of the pieces is equal to the number then it's a kaprekar number.
Example 1
INPUT:
p=1
Q=1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:
1,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBERS IS:8
/*Kaprekar numbers*/
import java.util.*;
class ISCprac2010q02{
public static void main(String args[])
throws InputMismatchException{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the range : ");
int p=scan.nextInt();
int q=scan.nextInt();
int d,i,n,a,b,s,freq;
freq=0;// to find the frequency of kaprekar numbers
System.out.println("The Kaprekar numbers are : ");
for(i=p;i<=q;i++)
{
n=i;
d=0;//to store the number of digits
//count the number of digits in the number
while(n>0){
d++;
n/=10;
}
s=i*i;// find the square of the number
//extract 'd' digits from the right of the square
//of the number
a=s%(int)Math.pow(10,d);
//extract 'd' or 'd-1' digits from the left of the square
//of the number
b=s/(int)Math.pow(10,d);
//Check if the two parts add up to the original number
//i.e. Condition for Kaprekar number
if(a+b==i){
System.out.print(i+" ");
freq++;
}
}
System.out.println("\nFREQUENCY OF KAPREKAR NUMBER IS : "
+freq);
}
}
Have something to say? Log in to comment on this post.
0 comments