阿姆斯特朗数字是一个等于其各个数字的立方之和的数字。例如,153是阿姆斯特朗数-
153 = (1)3 + (5)3 + (3)3 153 1 + 125 + 27 154 153
1. Take integer variable Arms 2. Assign value to the variable 3. Split all digits of Arms 4. Find cube-value of each digits 5. Add all cube-values together 6. Save the output to Sum variable 7. If Sum equals to Arms print Armstrong Number 8. If Sum not equals to Arms print Not Armstrong Number
import java.util.Scanner; public class ArmstrongNumber { public static void main(String args[]) { int number = 153; int check, rem, sum = 0; System.out.println("输入要验证的号码:"); Scanner sc = new Scanner(System.in); number = sc.nextInt(); check = number; while(check != 0) { rem = check % 10; sum = sum + (rem * rem * rem); check = check / 10; } if(sum == number) System.out.println("给定数字是一个阿姆斯壮数字。"); else System.out.println("给定数字不是阿姆斯壮数字。"); } }
输出结果
输入要验证的号码: 153 给定数字是一个阿姆斯壮数字。