I am writing a method to determine if a number is Armstrong for boot camp homework, and I am not allowed to use the Math class. Why is this not working?
public static boolean isArmstrong(int n){
String strNum = "" + n;
int pow = strNum.length();
int sum = 1;
for(int i = 0; i < strNum.length(); i++){
int eachDigit = Integer.parseInt(strNum.substring(i , i + 1));
for (int j = 0; j < pow; j++) {
sum *= eachDigit;
}
}
System.out.println(sum);
return sum == n;
}
public static void main(String[] args) {
System.out.println(isArmstrong(153));
}
prints:
3375
false
New contributor
JayBird is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.