亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> java

使用 For 循环的 JAVA 程序中的 Armstrong 数

什么是阿姆斯壮数?

在阿姆斯壮数中,各个数字的幂之和等于数字本身。

换句话说,以下等式将成立

xy..z = xn + yn+.....+ zn

n 是数字的位数

例如这是一个 3 位数的 Armstrong 号码

370 = 33 + 73 + o3
 = 27 + 343 + 0
 = 370

阿姆斯壮数的例子

 0, 1, 4, 5, 9, 153, 371, 407, 8208, etc.

让我们把它写在一个程序中:

检查数字是否为 Armstrong 数字的 Java 程序

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;
 
public class ArmstrongNumber {
 
	public static void main(String[] args) {
		
		 int inputArmstrongNumber = 153; //Input number to check armstrong  
		 int tempNumber, digit, digitCubeSum = 0;
 
	       tempNumber = inputArmstrongNumber;
	        while (tempNumber != 0)
	        {
	        	
	        	/* On each iteration, remainder is powered by thetempNumber of digits n
	        	 */
	            System.out.println("Current Number is "+tempNumber);
	            digit =tempNumber % 10;
				System.out.println("Current Digit is "+digit);
	            //sum of cubes of each digits is equal to thetempNumber itself
	            digitCubeSum = digitCubeSum + digit*digit*digit;
				System.out.println("Current digitCubeSum is "+digitCubeSum);
	            tempNumber /= 10;
	           
	        }
 
	        //check giventempNumber and digitCubeSum is equal to or not 
	        if(digitCubeSum == inputArmstrongNumber)
	            System.out.println(inputArmstrongNumber + " is an Armstrong Number");
	        else
	            System.out.println(inputArmstrongNumber + " is not an Armstrong Number");
 
	}
 
}

输出

Current Number is 153
Current Digit is 3
Current digitCubeSum is 27
Current Number is 15
Current Digit is 5
Current digitCubeSum is 152
Current Number is 1
Current Digit is 1
Current digitCubeSum is 153
153 is an Armstrong Number

用于打印从 0 到 999 的 Armstrong 数字的 Java 程序

//ChecktempNumber is Armstrong or not using while loop
package com.guru99;

public class ArmstrongNumber {

    public static void main(String[] args) {
        int tempNumber, digit, digitCubeSum;

        for (int inputArmstrongNumber = 0; inputArmstrongNumber < 1000; inputArmstrongNumber++) {
            tempNumber = inputArmstrongNumber;
            digitCubeSum = 0;
            while (tempNumber != 0) {

                /* On each iteration, remainder is powered by thetempNumber of digits n
                 */

                digit = tempNumber % 10;

                //sum of cubes of each digits is equal to thetempNumber itself
                digitCubeSum = digitCubeSum + digit * digit * digit;

                tempNumber /= 10;

            }

            //check giventempNumber and digitCubeSum is equal to or not 
            if (digitCubeSum == inputArmstrongNumber)
                System.out.println(inputArmstrongNumber + " is an Armstrong Number");

        }

    }

}

输出

0 is an Armstrong Number
1 is an Armstrong Number
153 is an Armstrong Number
370 is an Armstrong Number
371 is an Armstrong Number
407 is an Armstrong Number

java

  1. 使用钛平底锅的技巧
  2. C# for 循环
  3. Java Hello World 程序
  4. Java for-each 循环
  5. Java中的构造函数重载:什么是&程序示例
  6. 检查质数的Java程序
  7. 用Java打印从1到100的素数的程序
  8. Java中使用递归和循环程序的斐波那契数列
  9. 如何使用递归在 Java 中反转字符串
  10. Java中使用while和for循环的回文数程序
  11. Java中的插入排序算法及程序示例
  12. Java程序中的选择排序与示例