-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEgocentricNumbers.java
More file actions
40 lines (33 loc) · 953 Bytes
/
Copy pathEgocentricNumbers.java
File metadata and controls
40 lines (33 loc) · 953 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
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
public class EgocentricNumbers {
public static void main(String[] args) {
System.out.println(egocentricNumbers(5));
System.out.println(egocentricNumbers(153));
System.out.println(egocentricNumbers(9982));
System.out.println(egocentricNumbers(407));
}
private static int egocentricNumbers(int input) {
// find length of number
int n = 0;
int remainder = 0;
int in = input;
ArrayList<Integer> digits = new ArrayList<Integer>();
do {
remainder = input % 10;
input = input / 10;
digits.add(remainder);
n++;
} while(input > 0);
if(in == 0) {
n = 1;
}
int total = 0;
for (Integer digit: digits) {
total += Math.pow(digit, n);
}
if(total == in) {
return 1;
}
return 0;
}
}