Учебники

56) Проверьте номер Армстронга

Что такое номер Армстронга?

В числе Армстронга сумма степеней отдельных цифр равна самому числу.

Другими словами, справедливо следующее уравнение

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

n — количество цифр в номере

Например, это трехзначное число Армстронга

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

Примеры чисел Армстронга

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

Давайте напишем это в программе:

Java-программа, чтобы проверить, является ли число Армстронг номер

  1. //ChecktempNumber is Armstrong or not using while loop
  2. package com.guru99;
  3. public class ArmstrongNumber {
  4. public static void main(String[] args) {
  5. int inputArmstrongNumber = 153; //Input number to check armstrong
  6. int tempNumber, digit, digitCubeSum = 0;
  7. tempNumber = inputArmstrongNumber;
  8. while (tempNumber != 0)
  9. {
  10. /* On each iteration, remainder is powered by thetempNumber of digits n
  11. */
  12. System.out.println("Current Number is "+tempNumber);
  13. digit =tempNumber % 10;
  14. System.out.println("Current Digit is "+digit);
  15. //sum of cubes of each digits is equal to thetempNumber itself
  16. digitCubeSum = digitCubeSum + digit*digit*digit;
  17. System.out.println("Current digitCubeSum is "+digitCubeSum);
  18. tempNumber /= 10;
  19. }
  20. //check giventempNumber and digitCubeSum is equal to or not
  21. if(digitCubeSum == inputArmstrongNumber)
  22. System.out.println(inputArmstrongNumber + " is an Armstrong Number");
  23. else
  24. System.out.println(inputArmstrongNumber + " is not an Armstrong Number");
  25. }
  26. }

Вывод

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

Java-программа для печати чисел Армстронга от 0 до 999

  1. //ChecktempNumber is Armstrong or not using while loop
  2. package com.guru99;
  3.  
  4. public class ArmstrongNumber {
  5.  
  6. public static void main(String[] args) {
  7. int tempNumber, digit, digitCubeSum;
  8.  
  9. for (int inputArmstrongNumber = 0; inputArmstrongNumber < 1000; inputArmstrongNumber++) {
  10. tempNumber = inputArmstrongNumber;
  11. digitCubeSum = 0;
  12. while (tempNumber != 0) {
  13.  
  14. /* On each iteration, remainder is powered by thetempNumber of digits n
  15. */
  16.  
  17. digit = tempNumber % 10;
  18.  
  19. //sum of cubes of each digits is equal to thetempNumber itself
  20. digitCubeSum = digitCubeSum + digit * digit * digit;
  21.  
  22. tempNumber /= 10;
  23.  
  24. }
  25.  
  26. //check giventempNumber and digitCubeSum is equal to or not
  27. if (digitCubeSum == inputArmstrongNumber)
  28. System.out.println(inputArmstrongNumber + " is an Armstrong Number");
  29.  
  30. }
  31.  
  32. }
  33.  
  34. }

Вывод

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