Java程序检查给定数字是否为阿姆斯壮数字
阿姆斯特朗数字是一个等于其各个数字的立方之和的数字。例如,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 给定数字是一个阿姆斯壮数字。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短