Untitled Document
Untitled Document
Algorithm
STEP 1: Enter the limits
STEP 2: Run a loop using the entered limits
STEP 3: For each number in the range, count the number of
digits(d) in it
STEP 4: Find the square of the number
STEP 5: Now, divide the square in two parts
STEP 6: To do this, first store 10 raised to the power d in t
STEP 7: For first part, calculate square mod
STEP 8: For second part, calculate square/t
STEP 9: Now add the two parts
STEP 10: If the sum is equal to the original number then it is a
kaprekar number, print it and
count it.
STEP 11: If the sum is not equal to the original number it is
not a kaprekar number
STEP 12: Continue the process till all the numbers in the
range are checked
STEP 13: Display the frequency of kaprekar numbers.
STEP 14: End
Solution
import java.util.*;
class kap
{
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;
System.out.println("The Kaprekar numbers are: ");
for(i=p;i<=q;i++)
{
n=i;
d=0;
while(n>0)
d++;
n/=10;
}
s=i*i;
a=s%(int)Math.pow(10,d);
b=s/(int)Math.pow(10,d);
if(a+b==i)
{
System.out.print(i+" ");
freq++;
}
}
System.out.println("FREQUENCY OF KAPREKAR
NUMBER IS : "+freq);
}
}
Variable description
name type description
p int to take input
q int to take input
freq int to store frequency
i int loop variable
s int to store sum
d int to extract last digit
Output