-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathHappyNumbers.java
More file actions
28 lines (25 loc) · 802 Bytes
/
Copy pathHappyNumbers.java
File metadata and controls
28 lines (25 loc) · 802 Bytes
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
import java.util.*;
import java.io.*;
class Main {
public static boolean HappyNumbers(int num) {
String str = String.valueOf(num);
int sum = 0;
while (true) {
for (int i = 0; i < str.length(); i++) {
int n = Character.getNumericValue(str.charAt(i));
sum += (int)Math.pow(n, 2);
}
str = String.valueOf(sum);
sum = 0;
if (str.equals("1"))
return true;
else if (str.length() == 1 && !str.equals("1"))
return false;
}
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(HappyNumbers(s.nextLine()));
}
}