-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKaprekarNumber.java
55 lines (52 loc) · 1.01 KB
/
KaprekarNumber.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.Scanner;
public class KaprekarNumber {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter a number:");
int no=sc.nextInt();
if(isKaprekarNo(no)) {
System.out.println(no+" is Kaprekar Number");
}
else {
System.out.println(no+" is Not Kaprekar Number");
}
}
public static boolean isKaprekarNo(int no) {
if (no==1) {
return true;
}
else if(count(square(no))%2!=0) {
return false;
}
else if(splitAdd(count(square(no)),square(no)) == no) {
return true;
}
else {
return false;
}
}
public static int splitAdd(int count, int square) {
int den=base(count/2);
int no=(square/den)+(square%den);
return no;
}
public static int base(int count) {
int base=1;
while(count>0) {
base*=10;
count--;
}
return base;
}
public static int count(int square) {
int ct=0;
while(square>0) {
ct++;
square/=10;
}
return ct;
}
public static int square(int no) {
return no*no;
}
}