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 给定数字是一个阿姆斯壮数字。
热门推荐
1 泰迪狗狗如何训练
3 狗狗训练图
5 狗狗训练方法
6 训练狗狗不咬人
8 敏捷训练狗狗
10 训练狗狗吃饭
11 泰迪狗狗训练
12 狗狗从多大开始训练
13 狗狗训练学校费用
14 怎么去训练狗狗
15 狗狗训练狗狗
16 狗狗训练教程
17 训练狗狗装死
18 泰迪狗狗的训练方法