Program 16 : AMSTRONG
_____________________________________________
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
int originalNumber = number;
int sum = 0;
int digits = Integer.toString(number).length();
while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, digits);
number /= 10;
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is an
Armstrong number.");
} else {
System.out.println(originalNumber + " is not an
Armstrong number.");
}
}
}
ALGORITHM
_____________________________________________
Input the number.
Initialize sum to 0.
Calculate the number of digits in the number.
Extract each digit and raise it to the power equal to
the number of digits.
Add the result to sum.
Repeat steps 5-6 until all digits are processed.
If sum equals the original number, it is an Armstrong
number; otherwise, it is not.
OUTPUT
_____________________________________________
Variable Table
_____________________________________________