-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKaprekar.java
35 lines (35 loc) · 945 Bytes
/
Kaprekar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Programs;
import java.util.*;
class Kaprekar{
void accept(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the lower and upper limits");
int a=sc.nextInt();
int b=sc.nextInt();
int c=0;
System.out.println("The Kaprekar numbers are: ");
for(int i=a;i<=b;i++){
if(isKap(i)){
System.out.print(i+",");
c++;
}
}
System.out.println();
System.out.println("The frequency of the kaprekar numbers is: "+c);
}
boolean isKap(int p){
if(p==1)
return true;
int l=(int)(Math.log10(p)+1);
int m=(p*p)/(int)(Math.pow(10,l));
int n=(p*p)-(m*(int)(Math.pow(10,l)));
if((m*n)==p)
return true;
else
return false;
}
public static void main(String args[]){
Kaprekar ob=new Kaprekar();
ob.accept();
}
}