-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArmStrong.java
104 lines (58 loc) · 1.74 KB
/
ArmStrong.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
TCS - NQT - YOP 2024
Check element in linkedlist are Armstrong number
input:
12 34 153 2 1
output:
153
input:
12 756 42 232 4
output:
No Armstrong number present
*/
import java.util.LinkedList;
import java.util.Scanner;
public class ArmStrong {
public static double digits(int num){
int d=0;
while(num != 0){
d++;
num/=10;
}
return d;
}
public static boolean isArmStrong(int num){
int n=num;
double res=0;
while(num != 0){
int rem = num%10;
res = res + (int) Math.pow(rem,digits(n));
num/=10;
}
return (n == res)?true:false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<>();
LinkedList<Integer> res = new LinkedList<>();
System.out.println("Enter element spaces separated");
String input=sc.nextLine();
String[] elements = input.split(" ");
for (String ele : elements) {
list.add(Integer.parseInt(ele));
}
// System.out.prin?tln(list);
for (Integer ele : list) {
if (isArmStrong(ele)) {
res.add(ele);
}
}
if (!res.isEmpty()) {
System.out.println(res);
} else {
System.out.println("No Armstrong number present");
}
// System.out.println(isArmStrong(153));
// System.out.println(Math.pow(2, 3));
}
}