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

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

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

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

package com.beginnersbook;
import java.util.Scanner;

public class JavaExample
{
    public static void main(String args[])
    {
    int num, start, end, i, rem, temp, counter=0;

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter the start number: ");
    start = scanner.nextInt();
    System.out.print("Enter the end number: ");
    end = scanner.nextInt();
    scanner.close();

    //generate Armstrong numbers between start and end
    for(i=start+1; i<end; i++)
    {
       temp = i;
       num = 0;
       while(temp != 0)
       {
        rem = temp%10;
        num = num + rem*rem*rem;
        temp = temp/10;
       }
       if(i == num)
       {
        if(counter == 0)
        {
           System.out.print("Armstrong Numbers Between "+start+" and "+end+": ");
        }
           System.out.print(i + "  ");
           counter++;
       }
    }
    // if no Armstrong number is found
    if(counter == 0)
    {
       System.out.print("There is no Armstrong number Between "+start+" and "+end);
    }
    }
}

输出:

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

赞(0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Java 实例