Kaprekar
KAPREKAR
NUMBER
A positive whole number ‘n’ that has ‘d’ number of digits is squared and
split into two pieces. A right hand piece that has ‘d’ digit and a left hand
piece that was remaining ‘d’ or d-1 digits. If the sum of two pieces is equal
to the number ‘n’ is a kaprekar.The First Few Kaprekar numbers are:-9, 35, 297.
Example 1:- 9(given number).
(9)2=81, right hand piece=1
Left hand piece=8
Therefore, sum of right hand piece
and left hand piece=8+1=9(which is equal to the given number. Therefore, 9 is a
kaprekar number.
Given two positive integer p and q where p<q.Write a program to
determine how many kaprekar numbers are there in the range between p and g
(both inclusive) and output them. The input contains two positive integers p
and q. Assume p<5000 and g<5000. You have to output tg=he number of
kaprekar’s number in the specified range along with their values in the format
specified below.
Sample Data:- Input:- p=1, q=1000
Output: The
kaprekarnumbers are: 1, 9, 45, 55, 99, 279, 743, 999.
Frequency
of kaprekarnumbers:- 8.
ALGORITHM
STEP 1:- Start
STEP 2:- Declare kaprekar as a class. Declare and
initialize variables like p, q, i, sq, len, k, s, l, count. Now initialize
countß0
STEP 3:- Declare ‘input’ as a method with return type
void. Now input lower range in ‘p’. Now input upper range in ‘q’.
STEP 4:- Now check ‘p’>=5000 or ‘q’>=5000 or
‘p’>=’q’. If the condition is satisfy, then showing the message “OUT OF
RANGE”. Otherwise find kaprekar numbers.
STEP 5:- Start iteration on ‘i’. The value of ‘p’ is
assigned on ‘i’ and iteration rotate upto ‘q’ with increment 1 in each rotation.
STEP 6:- Declare a method check with parameter ‘n’ of
type int and return type is also int.
STEP 7:- Find the length of ‘n’ and store it into
‘len’. Assign the square value of n and store it into ‘sq’.
STEP 8:- Now finding the right hand piece and left hand
piece of the number after squaring
STEP 9:- Now store the right hand piece of the number
into ‘k’ and left piece of the number into ‘s’. Now sum of ‘k’ and ‘s’ and
store the result into ‘n’.
STEP 10:- Check whether ‘l’ and ‘n’ both are same or
not. If same then return 1 otherwise return 0.
STEP 11:- Now call the check method with parameter
within input() and compare whether it is equal to 1.
STEP 12:- Now check whether count is zero or not, if
zero then display ‘i’ values otherwise print kaprekar number within the range.
Also print the frequency of the kaprekar number within the range. Now close
input method()
STEP 13:- Declare main() method. Create the object ob of
class kaprekar, call the input() method through object ob.
STEP 14:- Stop.
SOURCECODE
import java.util.*;
class kaprekar{
int p,q,i,len,sq,k,s,l,count=0;
Scanner sc =new Scanner(System.in);
void input(){
System.out.println("ENTER
THE LOWER AND UPPER RANGE:");
p=sc.nextInt();
q=sc.nextInt();
if(p>=5000||q>=5000||p>=q)
System.out.println("OUT
OF RANGE");
else{
System.out.println("THE
KAPREKAR NUMBER ARE=");
for(i=p;i<=q;i++){
if(check(i)==1){
if(count==0)
System.out.print(i);
else
System.out.print(","+i);
count++;}}
System.out.println();
System.out.println("FREQUENCY
OF KAPREKAR NUMBER IS:"+count)}}
int check(int n){
len=(""+n).length();
sq=n*n;
k=sq%(int)(Math.pow(10,len));//finding
right hand piece
s=sq/(int)(Math.pow(10,len));//finding
left hand piece
l=k+s;
if(l==n)
return 1;
else
return 0;}
public static void main(){
kaprekarob=new kaprekar();
ob.input();}}
VARIABLEDESCRIPTION
S.L.NO. |
NAME |
DATA TYPE |
DESCRIPTION |
1 |
p |
int |
It will hold the lower range. |
2 |
q |
int
|
It will hold the upper range. |
3 |
i |
int |
It is used as a loop control
variable. |
4 |
n |
int |
It is passing as a formal
parameter to calculate the square of the each value within loop. |
5 |
len |
int
|
It is used to store the length
of the number. |
6 |
sq |
int |
It is used to store the square
value of the number. |
7 |
k |
int |
It is used to finding and store
right hand piece value. |
8 |
s |
int
|
It is used to finding and store
left hand piece value. |
9 |
l |
int |
It is used to store the
addition value of right hand piece and left hand piece. |
10 |
count |
int |
It is the counter to count the
kaprekar number within the range. |
INPUTANDOUTPUT
