Armstrong program in Java.



       An Armstrong number is a number such that the sum
of its digits raised to the third power is equal to the number
itself.  For example, 153 is an Armstrong number, since
1*1*1 + 5*5*5 + 3*3*3 = 153. 
public class First {
void armstrong(int num) {
int c = 0, temp;
temp = num;
while (num != 0) {
int d = num % 10;
num = num / 10;
c = c + (int) (Math.pow(d, 3));
}
if (temp == c) {
System.out.println("armstrong");
} else {
System.out.println("Not armstrong");
}
}
public static void main(String[] args) {
new First().armstrong(153);
}
}

No comments: