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 生病祝福语简短文案