-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArmstrongApp.java
41 lines (32 loc) · 1.05 KB
/
ArmstrongApp.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
package gr.aueb.cf.projects;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;
public class ArmstrongApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
final ArrayList<Integer> digits = new ArrayList<>();
int inputNumber;
int digitsCount = 0;
int sum = 0;
int num = 0;
int digit = 0;
boolean isArmstrong = false;
System.out.println("Please insert a number");
inputNumber = in.nextInt();
num = inputNumber;
do {
digitsCount++;
digit = num % 10;
digits.add(digit);
num = num / 10;
} while (num!= 0);
for (int item : digits) {
sum += Math.pow(item, digitsCount);
}
isArmstrong = (sum == inputNumber);
System.out.println("Number is: " + inputNumber);
System.out.println("Sum is: " + sum);
System.out.printf("%d is Armstrong: %s", inputNumber, (isArmstrong) ? "YES" : "FALSE");
}
}