Write a program in Java to display the armstrong numbers formed by the combination of the factors of the first ten natural numbers using the method prototype: String num(int a)
package amstrong;
public class Amstrong {
public static boolean isArmstrongNumber(int num) {
int total = 0, right, t;
t = num;
while (t != 0) {
right = t % 10;
total = total + (right * right * right);
t = t / 10;
}
if (total == num) {
return true;
} else {
return false;
}
}
public static String num(int a){
String factors = " ";
for(int i=1; i<=a; i++){
String se = " ";
for(int j=1; j<i; j++){
if(i%j==1){
se += String.valueOf(j);
}
}
int k = Integer.parseInt(se);
if(isArmstrongNumber(k)){
factors += String.valueOf(k) +" ";
}
}
return factors;
}
public static void main(String[] args) {
System.out.println(num(10));
}
}
Comments
Leave a comment