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 数字

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 示例