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 给定数字是一个阿姆斯壮数字。
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短