原文: https://beginnersbook.com/2019/02/java-program-to-print-armstrong-numbers-between-a-given-range/

我们已经看过 java 程序来检查 Armstrong 数字。在本教程中,我们将编写一个 java 程序来打印给定范围之间的 Armstrong 数字。

Java 示例:在给定范围之间打印 Armstrong 数字

在此程序中,要求用户输入起始和结束数字,程序然后在这些输入数字之间打印 Armstrong 数字。

  1. package com.beginnersbook;
  2. import java.util.Scanner;
  3. public class JavaExample
  4. {
  5. public static void main(String args[])
  6. {
  7. int num, start, end, i, rem, temp, counter=0;
  8. Scanner scanner = new Scanner(System.in);
  9. System.out.print("Enter the start number: ");
  10. start = scanner.nextInt();
  11. System.out.print("Enter the end number: ");
  12. end = scanner.nextInt();
  13. scanner.close();
  14. //generate Armstrong numbers between start and end
  15. for(i=start+1; i<end; i++)
  16. {
  17. temp = i;
  18. num = 0;
  19. while(temp != 0)
  20. {
  21. rem = temp%10;
  22. num = num + rem*rem*rem;
  23. temp = temp/10;
  24. }
  25. if(i == num)
  26. {
  27. if(counter == 0)
  28. {
  29. System.out.print("Armstrong Numbers Between "+start+" and "+end+": ");
  30. }
  31. System.out.print(i + " ");
  32. counter++;
  33. }
  34. }
  35. // if no Armstrong number is found
  36. if(counter == 0)
  37. {
  38. System.out.print("There is no Armstrong number Between "+start+" and "+end);
  39. }
  40. }
  41. }

输出:

Java 程序:在给定范围之间打印 Armstrong 数字 - 图1

相关的 Java 示例

  1. Java 程序:打印 Pascal 三角形
  2. Java 程序:将整数分解为数字
  3. Java 程序:打印给定范围之间的素数
  4. Java 程序:生成随机数