- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathHappyNumbers.java
28 lines (25 loc) · 802 Bytes
/
HappyNumbers.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
importjava.util.*;
importjava.io.*;
classMain {
publicstaticbooleanHappyNumbers(intnum) {
Stringstr = String.valueOf(num);
intsum = 0;
while (true) {
for (inti = 0; i < str.length(); i++) {
intn = Character.getNumericValue(str.charAt(i));
sum += (int)Math.pow(n, 2);
}
str = String.valueOf(sum);
sum = 0;
if (str.equals("1"))
returntrue;
elseif (str.length() == 1 && !str.equals("1"))
returnfalse;
}
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(HappyNumbers(s.nextLine()));
}
}