forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeith_Number.java
47 lines (42 loc) · 1.03 KB
/
Keith_Number.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
import java.util.*;
class KeithNumberExample1
{
static boolean isKeith(int x)
{
ArrayList<Integer> terms=new ArrayList<Integer>();
int temp = x, n = 0;
while (temp > 0)
{
terms.add(temp%10);
temp = temp/10;
n++;
}
//reverse the List
Collections.reverse(terms);
int next_term = 0, i = n;
while (next_term < x)
{
next_term = 0;
for (int j=1; j<=n; j++)
next_term = next_term + terms.get(i-j);
terms.add(next_term);
i++;
}
return (next_term == x);
}
public static void main(String[] args)
{
if (isKeith(19))
System.out.println("Yes, the given number is a Keith number.");
else
System.out.println("No, the given number is not a Keith number.");
if(isKeith(742))
System.out.println("Yes, the given number is a Keith number.");
else
System.out.println("No, the given number is not a Keith number.");
if(isKeith(4578))
System.out.println("Yes, the given number is a Keith number.");
else
System.out.println("No, the given number is not a Keith number.");
}
}