-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberAmstrong.java
More file actions
33 lines (22 loc) · 814 Bytes
/
NumberAmstrong.java
File metadata and controls
33 lines (22 loc) · 814 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
public class NumberAmstrong{
public static void main(String[]args){
System.out.print(isAmstrong(153));
}
public static int numberOfDigits(int number){
String numberConvert = Integer.toString(number);
return numberConvert.length();
}
//method to find if number is Amstrong
public static boolean isAmstrong(int userInput){
int sum = 0;
int digits = numberOfDigits(userInput);
int value = userInput;
while(value > 0){
int lastDigit = value % 10;
double raised = Math.pow(lastDigit, digits);
sum += raised;
value = value/10;
}
return sum == userInput;
}
}