Sunday, March 8, 2020

BlueJ Program On Kaprekar Numbers


Given the two positive integers p and q, where p kaprekar numbers
 are there in the range between 'p' and 'q'(both inclusive )and output them.About 'kaprekar' number:

A posotive 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.

SAMPLE DATA:
INPUT:
p=1
Q=1000
OUTPUT:
THE KAPREKAR NUMBERS ARE:
1,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBERS IS:8





import java.io.*;
class Bank
{
int i,p,q,c=0;
int num;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{

System.out.println("Enter the Lower Range:"); p=Integer.parseInt(br.readLine()); System.out.println("Enter the Upper Range:"); q=Integer.parseInt(br.readLine()); if(p >=q)

{
System.out.println("Wrong Entry...");
return;
}
System.out.println("THE KAPREKAR NUMBERS ARE:");
for(i=p;i<=q;i++)
{
show(i);
}
System.out.println("FREQUENCY OF KAPREKAR NUMBERS IS:"+c);
}
public void show(int x)
{
int digit,rev=0;int no;num=x*x;digit=0;no=x;
while(no >0)
{
digit++;
no=no/10;
}
no=num;
while(digit >0)
{
rev=rev*10+no%10;
no=no/10;
digit--;
} 
//  'rev' holds the right part in reverse order and 'no' holds the left part rev=reverse(rev);
if((rev+no)==x)
{
System.out.print(" "+x);
c++;
}
}
private int reverse(int n)
{
int r=0;
while(n >0)
{
r=r*10+n%10;
n=n/10;
}
return r;
}
}


Back To 2010 Computer Practical Paper: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner