0% found this document useful (0 votes)
6 views

Program 3

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Program 3

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Program 3 Kaprekar number or not

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:
297 is a Kaprekar number for base 10. because 297² = 88209, which can
be split into 88 and 209, and 88 + 209 = 297. By convention, the second part
may start with the digit 0, but must be positive. For example, 999 is a Kaprekar
number for base 10, because 999² = 998001, which can be split into 998 and
001, and 998 + 001 = 999. But 100 is not; although 100² = 10000 and 100 + 00 =
100, the second part here is not positive.
SAMPLE DATA:
INPUT: p=1
Q=1000
OUTPUT: THE KAPREKAR NUMBERS ARE:
1,9,45,55,99,297,703,999
FREQUENCY OF KAPREKAR NUMBERS IS:8
class Kaprekar
{
int is_kar(int n)
{
int I,rem,cnt=0,rev=0,temp=n;
int sq=n*n;
while(n>0)
{
rem=n%10;
cnt++;
n=n/10;
}
int div=(int)Math.pow(10,cnt);
int second=sq%div;
int first=sq/div;
if(first+second==temp)
{
return 1;
}
else
{
return 0;
}
}
public static void main(int p.int q)
{
Kaprekar ob1=new Kaprekar();
int i,count=0;
System.out.println("THE KAPREKAR NUMBERS ARE:");
for(i=p;i<=q;i++)
{
int R=ob1.is_kar(i);
if(R==1)
{
System.out.print(i+”,”);
count++;
}
}
System.out.println("\n FREQUENCY OF KAPREKAR NUMBERS IS:"+count);
}
}

You might also like